When to use flags
This article mainly explains "when to use flags". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "when to use flags".
Possible flags: (available flags:)
Once: make sure the callback list only executes (.fire ()) once (like a deferred Deferred).
Memory: keep the previous value and immediately invoke any callback (like a deferred Deferred) with the latest value added to the list.
Unique: make sure you can only add one callback at a time (so there are no duplicate callbacks in the list).
StopOnFalse: interrupts the call when a callback returns false
By default, the callback list can be triggered multiple times as in the callback list of events.
For an example of how flags should be used ideally, see below:
$.Callboxes ("once"):
Var callbacks = $.Callboxes ("once")
Callbacks.add (fn1)
Callbacks.fire ("foo")
Callbacks.add (fn2)
Callbacks.fire ("bar")
Callbacks.remove (fn2)
Callbacks.fire ("foobar")
/ *
Output:
Foo
* /
$.Callboxes ("memory"):
Var callbacks = $.Callboxes ("memory")
Callbacks.add (fn1)
Callbacks.fire ("foo")
Callbacks.add (fn2)
Callbacks.fire ("bar")
Callbacks.remove (fn2)
Callbacks.fire ("foobar")
/ *
Output:
Foo
Fn2 says:foo
Bar
Fn2 says:bar
Foobar
* /
$.Callboxes ("unique"):
Var callbacks = $.Callboxes ("unique")
Callbacks.add (fn1)
Callbacks.fire ("foo")
Callbacks.add (fn1); / / repeat addition
Callbacks.add (fn2)
Callbacks.fire ("bar")
Callbacks.remove (fn2)
Callbacks.fire ("foobar")
/ *
Output:
Foo
Bar
Fn2 says:bar
Foobar
* /
$.Callboxes ("stopOnFalse"):
Function fn1 (value) {
Console.log (value)
Return false
}
Function fn2 (value) {
Fn1 ("fn2 says:" + value)
Return false
}
Var callbacks = $.Callboxes ("stopOnFalse")
Callbacks.add (fn1)
Callbacks.fire ("foo")
Callbacks.add (fn2)
Callbacks.fire ("bar")
Callbacks.remove (fn2)
Callbacks.fire ("foobar")
/ *
Output:
Foo
Bar
Foobar
* /
Thank you for your reading, the above is the content of "when to use flags", after the study of this article, I believe you have a deeper understanding of when to use flags, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!