node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

es6-dispatcher

es6-dispatcher

Build Status

Tiny hooks implementationm based on Co & ES6.

Better than redispatcher

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