eventproxy Version: 0.3.1 By @Jackson Tian <shyvo1987@gmail.com>

An implementation of task/event based asynchronous pattern.

eventproxy: API索引


EventProxy

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

addListener

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.

bind

addListener alias, bind

属性 EventProxy.prototype.bind bind

on

addListener alias, on

属性 EventProxy.prototype.on on

subscribe

addListener alias, subscribe

属性 EventProxy.prototype.subscribe subscribe

headbind

Bind an event, but put the callback into head of all callbacks.

方法 EventProxy.prototype.headbind() headbind
参数 eventname(String) Event name.
参数 callback(Function) Callback.

removeListener

Remove one or many callbacks.

  • If callback is null, removes all callbacks for the event.
  • If eventname is null, removes all bound callbacks for all events.
方法 EventProxy.prototype.removeListener() removeListener
参数 eventname(String) Event name.
参数 callback(Function) Callback.

unbind

removeListener alias, unbind

属性 EventProxy.prototype.unbind unbind

removeAllListeners

Remove all listeners. It equals unbind()
Just add this API for as same as Event.Emitter.

方法 EventProxy.prototype.removeAllListeners() removeAllListeners
参数 event(String) Event name.

bindForAll

Bind the ALL_EVENT event

方法 EventProxy.prototype.bindForAll() bindForAll

unbindForAll

Unbind the ALL_EVENT event

方法 EventProxy.prototype.unbindForAll() unbindForAll

trigger

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

emit

trigger alias

属性 EventProxy.prototype.emit emit

fire

trigger alias

属性 EventProxy.prototype.fire fire

once

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

emitLater
make emit async

方法 EventProxy.prototype.emitLater() emitLater

immediate

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.

asap

immediate alias

属性 EventProxy.prototype.asap asap

all

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.

assign

all alias

属性 EventProxy.prototype.assign assign

fail

Assign the only one 'error' event handler.

方法 EventProxy.prototype.fail() fail
参数 callback(Function(err))

tail

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.

assignAll

tail alias, assignAll

属性 EventProxy.prototype.assignAll assignAll

assignAlways

tail alias, assignAlways

属性 EventProxy.prototype.assignAlways assignAlways

after

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.

group

The after method's helper. Use it will return ordered results.
If you need manipulate result, you need callback
Examples:

var ep = new EventProxy();
ep.after('file', files.length, function (list) {
  // Ordered results
});
for (var i = 0; i < files.length; i++) {
  fs.readFile(files[i], 'utf-8', ep.group('file'));
}
方法 EventProxy.prototype.group() group
参数 eventname(String) Event name, shoule keep consistent with `after`.
参数 callback(Function) Callback function, should return the final result.

any

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.

not

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.

done

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

doneLater

make done async

方法 EventProxy.prototype.doneLater() doneLater
返回 Function delay done

create

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