четверг, 7 ноября 2013 г.

Micro Event Library

/**
 * MicroEvent.js debug
 *
 * # it is the same as MicroEvent.js but it adds checks to help you debug
*/

var MicroEvent = function(){}
MicroEvent.prototype = {
bind : function(event, fct){
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(fct);
},
unbind : function(event, fct){
console.assert(typeof fct === 'function');
this._events = this._events || {};
if( event in this._events === false  ) return;
console.assert(this._events[event].indexOf(fct) !== -1);
this._events[event].splice(this._events[event].indexOf(fct), 1);
},
trigger : function(event /* , args... */){
this._events = this._events || {};
if( event in this._events === false  ) return;
for(var i = 0; i < this._events[event].length; i++){
this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1))
}
}
};

/**
 * mixin will delegate all MicroEvent.js function in the destination object
 *
 * - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent
 *
 * @param {Object} the object which will support MicroEvent
*/

MicroEvent.mixin = function(destObject){
var props = ['bind', 'unbind', 'trigger'];
for(var i = 0; i < props.length; i ++){
if( typeof destObject === 'function' ){
destObject.prototype[props[i]] = MicroEvent.prototype[props[i]];
}else{
destObject[props[i]] = MicroEvent.prototype[props[i]];
}
}
}

// export in common js

if( typeof module !== "undefined" && ('exports' in module)){
module.exports = MicroEvent
}


Example

var Ticker = function() {
  var self = this;
  setInterval(function() {
    // 'trigger' is provided by MicroEvent
    self.trigger('tick', new Date());
  }, 1000);
};

// You have to use this method to make your class MicroEvent-enabled:

MicroEvent.mixin(Ticker);

var ticker = new Ticker();

// Now we can observe when the event fires

ticker.bind('tick', function(date) {
  console.log('notified date', date);
});

First we define the class which gonna use MicroEvent.js. This is a ticker, it is triggering 'tick' event every second, and add the current date as parameter

var Ticker = function(){
    var self = this;
    setInterval(function(){
        self.trigger('tick', new Date());
    }, 1000);
};

We mixin MicroEvent into Ticker and we are all set.

MicroEvent.mixin(Ticker);

Now lets actually use the Ticker Class. First, create the object.

var ticker = new Ticker();

and bind our tick event with its data parameter

ticker.bind('tick', function(date) {
    console.log('notified date', date);
});

And you will see this output:

notified date Tue, 22 Mar 2011 14:43:41 GMT
notified date Tue, 22 Mar 2011 14:43:42 GMT

Комментариев нет:

Отправить комментарий