2.1.0 • Published 9 years ago
es6-dispatcher v2.1.0
es6-dispatcher
Tiny hooks implementationm based on Co & ES6.
Installation
You should compile this module to regular js with something like babel' cause it is using ES6 syntax
npm i es6-dispatcher
API
let use = new Dispatcher();
use('init', () => console.log('init hook')); // register init hook
use('init'); // dispatch init hook
Examples
Tiny example to get the idea.
var Backbone = require('backbone');
var Dispatcher = require('es6-dispatcher');
var mixin = require('es6-class-mixin');
class Model extends mixin(Backbone.Model, Dispatcher.mixin) {
constructor() {
// convert data before save
this.use('save:before', () => {
this.set({ hello_arguments: {
data: this.get('hello')
}});
});
// reload page after save
this.use('save:after', () => {
window.location.reload();
});
super();
}
save(...args) {
// return promise
return co.call(this, function * () {
yield this.use('save:before');
this.save(...args);
yield this.use('save:after');
});
}
}
let view = new Model({ hello: 'world' });
Model.save();
License
MIT