An implementation of task/event based asynchronous pattern.
global define
EventProxy. An implementation of task/event based asynchronous pattern.
A module that can be mixed in to any object in order to provide it with custom events.
You may bind
or unbind
a callback function to an event;trigger
-ing an event fires all callbacks in succession.
Examples:
var render = function (template, resources) {};
var proxy = new EventProxy();
proxy.assign("template", "l10n", render);
proxy.trigger("template", template);
proxy.trigger("l10n", resources);
函数 | EventProxy() | EventProxy |
Bind an event, specified by a string name, ev
, to a callback
function.
Passing ALL_EVENT will bind the callback to all events fired.
Examples:
var proxy = new EventProxy();
proxy.addListener("template", function (event) {
// TODO
});
方法 | EventProxy.prototype.addListener() | addListener | |
参数 | eventname(String) | Event name. | |
参数 | callback(Function) | Callback. |
addListener
alias, bind
属性 | EventProxy.prototype.bind | bind |
addListener
alias, on
属性 | EventProxy.prototype.on | on |
addListener
alias, subscribe
属性 | EventProxy.prototype.subscribe | subscribe |
Bind an event, but put the callback into head of all callbacks.
方法 | EventProxy.prototype.headbind() | headbind | |
参数 | eventname(String) | Event name. | |
参数 | callback(Function) | Callback. |
Remove one or many callbacks.
callback
is null, removes all callbacks for the event.eventname
is null, removes all bound callbacks for all events.方法 | EventProxy.prototype.removeListener() | removeListener | |
参数 | eventname(String) | Event name. | |
参数 | callback(Function) | Callback. |
removeListener
alias, unbind
属性 | EventProxy.prototype.unbind | unbind |
Remove all listeners. It equals unbind()
Just add this API for as same as Event.Emitter.
方法 | EventProxy.prototype.removeAllListeners() | removeAllListeners | |
参数 | event(String) | Event name. |
Bind the ALL_EVENT event
方法 | EventProxy.prototype.bindForAll() | bindForAll |
Unbind the ALL_EVENT event
方法 | EventProxy.prototype.unbindForAll() | unbindForAll |
Trigger an event, firing all bound callbacks. Callbacks are passed the
same arguments as trigger
is, apart from the event name.
Listening for "all"
passes the true event name as the first argument.
方法 | EventProxy.prototype.trigger() | trigger | |
参数 | eventname(String) | Event name | |
参数 | data(Mix) | Pass in data |
trigger
alias
属性 | EventProxy.prototype.emit | emit |
trigger
alias
属性 | EventProxy.prototype.fire | fire |
Bind an event like the bind method, but will remove the listener after it was fired.
方法 | EventProxy.prototype.once() | once | |
参数 | ev(String) | Event name | |
参数 | callback(Function) | Callback |
emitLater
make emit async
方法 | EventProxy.prototype.emitLater() | emitLater |
Bind an event, and trigger it immediately.
方法 | EventProxy.prototype.immediate() | immediate | |
参数 | ev(String) | Event name. | |
参数 | callback(Function) | Callback. | |
参数 | data(Mix) | The data that will be passed to calback as arguments. |
immediate
alias
属性 | EventProxy.prototype.asap | asap |
Assign some events, after all events were fired, the callback will be executed once.
Examples:
proxy.all(ev1, ev2, callback);
proxy.all([ev1, ev2], callback);
proxy.all(ev1, [ev2, ev3], callback);
方法 | EventProxy.prototype.all() | all | |
参数 | eventname1(String) | First event name. | |
参数 | eventname2(String) | Second event name. | |
参数 | callback(Function) | Callback, that will be called after predefined events were fired. |
all
alias
属性 | EventProxy.prototype.assign | assign |
Assign the only one 'error' event handler.
方法 | EventProxy.prototype.fail() | fail | |
参数 | callback(Function(err)) |
Assign some events, after all events were fired, the callback will be executed first time.
Then any event that predefined be fired again, the callback will executed with the newest data.
Examples:
proxy.tail(ev1, ev2, callback);
proxy.tail([ev1, ev2], callback);
proxy.tail(ev1, [ev2, ev3], callback);
方法 | EventProxy.prototype.tail() | tail | |
参数 | eventname1(String) | First event name. | |
参数 | eventname2(String) | Second event name. | |
参数 | callback(Function) | Callback, that will be called after predefined events were fired. |
tail
alias, assignAll
属性 | EventProxy.prototype.assignAll | assignAll |
tail
alias, assignAlways
属性 | EventProxy.prototype.assignAlways | assignAlways |
The callback will be executed after the event be fired N times.
方法 | EventProxy.prototype.after() | after | |
参数 | eventname(String) | Event name. | |
参数 | times(Number) | N times. | |
参数 | callback(Function) | Callback, that will be called after event was fired N times. |
The callback will be executed after any registered event was fired. It only executed once.
方法 | EventProxy.prototype.any() | any | |
参数 | eventname1(String) | Event name. | |
参数 | eventname2(String) | Event name. | |
参数 | callback(Function) | The callback will get a map that has data and eventname attributes. |
The callback will be executed when the event name not equals with assigned event.
方法 | EventProxy.prototype.not() | not | |
参数 | eventname(String) | Event name. | |
参数 | callback(Function) | Callback. |
Success callback wrapper, will handler err for you.
fs.readFile('foo.txt', ep.done('content'));
// equal to =>
fs.readFile('foo.txt', function (err, content) {
if (err) {
return ep.emit('error', err);
}
ep.emit('content', content);
});
fs.readFile('foo.txt', ep.done('content', function (content) {
return content.trim();
}));
// equal to =>
fs.readFile('foo.txt', function (err, content) {
if (err) {
return ep.emit('error', err);
}
ep.emit('content', content.trim());
});
方法 | EventProxy.prototype.done() | done | |
参数 | handler,(Function,String) | success callback or event name will be emit after callback. | |
返回 | Function |
make done async
方法 | EventProxy.prototype.doneLater() | doneLater | |
返回 | Function | delay done |
Create a new EventProxy
Examples:
var ep = EventProxy.create();
ep.assign('user', 'articles', function(user, articles) {
// do something...
});
// or one line ways: Create EventProxy and Assign
var ep = EventProxy.create('user', 'articles', function(user, articles) {
// do something...
});
方法 | EventProxy.create() | EventProxy | create |
返回 | EventProxy | EventProxy instance |