EventDispatcher

Matt Sweetman edited this page May 9, 2013 · 3 revisions

EventDispatcher

EventDispatcher is an extension to BaseClass that adds Backbone.Events functionality. Simply use it at the root of your class inheritance:

var Wand = require('wand');

// Extend EventDispatcher like any other Backbone class:
var MyClass = Wand.EventDispatcher.extend({
  doSomething: function() {
    // All the Backbone.Events functions are now available to the class
    this.trigger('something');
  }
});

var myClass = new MyClass();
myClass.on('something', function() {
  console.log('did something');
});

myClass.doSomething(); // will log 'did something'

Essentially EventDispatcher is just a shortcut to extending each of your classes with Backbone.Events. You can achieve the same thing by extending each of your classes like so:

_.extend(MyClass.prototype, Backbone.Events);