Skip to content

Instantly share code, notes, and snippets.

@zoxon
Forked from Takazudo/events.js
Created February 17, 2018 14:00
Show Gist options
  • Save zoxon/4ccab741e195e2a6f7dd5e162b8dc399 to your computer and use it in GitHub Desktop.
Save zoxon/4ccab741e195e2a6f7dd5e162b8dc399 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