Events/A
From CommonJS Spec Wiki
< Events
STATUS: PROPOSAL
Contents
Proposal
NameEmitter
- NameEmitter() NameEmitter
- creates a name emitter with an empty internal emitter memo.
- observe(name String, observer Function) Emitter
- gets or creates an internal emitter with the given name. Calls and returns the result of the "observe" method on the internal emitter, passing the given observer function.
- emit(name String, ...args) Undefined
- gets or creates an internal emitter with the given name. Applies and returns the result of the "emit" method on the internal emitter, passing the rest of the arguments.
- emitter(name String) Emitter
- gets or creates an internal emitter with the given name.
Emitter
- Emitter(defaultAction_opt Function, dismiss_opt Function) Emitter
- creates a emitter with an optional default action and an empty observer array.
- observe(observer Function) Emitter
- creates a new Emitter from the given action and a dismissal function, and pushes that emitter onto an internal array of observers. The dismissal function, when called, removes the child emitter from the parent emitter's array of observers.
- emit(...args) Undefined
- uses "this" as an "Event" instance, creating a new one if necessary, and iteratively emits the event as the this object and the given arguments to each of its own observers until the event says it should no longer propagate, and then calls and returns the result of the default action if the event says it should still default.
- dismiss() Undefined
- if the emitter was constructed with a dismissal function, calls that function.
Event
- Event()
- creates a new Event that should still propagate and cause the corresponding emitter's default action to occur
- stopPropagation() Undefined
- prevents the event from propagating to further observers
- cancelDefault() Undefined
- cancels the corresponding emitter's default action
- stop() Undefined
- stops propagation and cancels the default action
- propagating() Boolean
- returns whether the event should continue propagating to further observers
- defaulting() Boolean
- returns whether the default action of the corresponding emitter should be called
Examples
Dismissing an Observer
var nameEmitter = new NameEmitter(); var emitter = nameEmitter.observe("foo", function () { }); emitter.dismiss();
Observing Before Another Observer
var parent = nameEmitter.observe("foo", function () {print("parent")}); var child = parent.observe(function () {print("child")}); var beforeChild = child.observe(function () {print("beforeChild");}); nameEmitter.emit("foo");
beforeChild child parent
Observing After an Observer
var parent = new Emitter(function () {print("parent")}); var child = parent.observe(function () {print("child");}); var afterChild = parent.observe(function () {print("afterChild")}); parent.emit();
child afterChild parent
Emit Arguments
var emitter = new Emitter(function (a, b, c) { print(a, b, c); }); emitter.emit(1, 2, 3);
1 2 3