Created
July 31, 2012 17:45
-
-
Save Takazudo/3218893 to your computer and use it in GitHub Desktop.
lazy event implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eventmodule */ | |
/* Events */ | |
var Events = { | |
on: function(events, callback) { | |
if(!this._observer) { | |
this._observer = $({}); | |
} | |
this._observer.bind(events, callback); | |
return this; | |
}, | |
trigger: function(events, params) { | |
if(!this._observer) { | |
return this; | |
} | |
this._observer.trigger(events, params); | |
return this; | |
} | |
}; | |
/* impl */ | |
var ConcreteCls = function(){ | |
// constructor I am. | |
}; | |
ConcreteCls.prototype.on = Events.on; | |
ConcreteCls.prototype.trigger = Events.trigger; | |
/* do it */ | |
var instance = new ConcreteCls; | |
instance.on('foo', function(e, params){ alert(params.val); }); | |
instance.trigger('foo', { val: 'yes!' }); // yes! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment