Skip to content

Instantly share code, notes, and snippets.

@j0lvera
Forked from addyosmani/backboneglobalpubsub.md
Created December 25, 2013 02:37
Show Gist options
  • Save j0lvera/8119755 to your computer and use it in GitHub Desktop.
Save j0lvera/8119755 to your computer and use it in GitHub Desktop.

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