framework/js/lib/utils/evented.js

37 lines
583 B
JavaScript
Raw Normal View History

2015-04-25 20:58:39 +08:00
export default {
handlers: null,
/**
*/
getHandlers(event) {
this.handlers = this.handlers || {};
return this.handlers[event] = this.handlers[event] || [];
},
/**
*/
trigger(event, ...args) {
this.getHandlers(event).forEach((handler) => handler.apply(this, args));
},
/**
*/
on(event, handler) {
this.getHandlers(event).push(handler);
},
/**
*/
off(event, handler) {
var handlers = this.getHandlers(event);
var index = handlers.indexOf(handler);
if (index !== -1) {
handlers.splice(index, 1);
}
}
}