Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created July 31, 2012 17:45
Show Gist options
  • Save Takazudo/3218893 to your computer and use it in GitHub Desktop.
Save Takazudo/3218893 to your computer and use it in GitHub Desktop.
lazy event implementation
/* 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