Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tiborsaas/2829892 to your computer and use it in GitHub Desktop.
Save tiborsaas/2829892 to your computer and use it in GitHub Desktop.
Backbone.js global pub/sub

Generally one implements notifications by listening for events on specific models but if one wishes to have a single global message interchange, it could be done as follows:

var pubsub = new Backbone.Model;

View1 = Backbone.View.extend({
  initialize: function(){
    pubsub.bind('custom event', callback);
  }
  // ...
});

View2 = Backbone.View.extend({
  // ...
  foo: function(){
    pubsub.trigger('custom event', data);
  }
});

or the reverse if one wished to publish events from models or collections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment