Skip to content

Instantly share code, notes, and snippets.

@davemo
Created October 7, 2011 13:38
Show Gist options
  • Save davemo/1270297 to your computer and use it in GitHub Desktop.
Save davemo/1270297 to your computer and use it in GitHub Desktop.
Using backbone custom events and the observer pattern
// app.js, ommitting IIFE's for brevity
APP = {};
_.extend(APP, Backbone.Events);
// views.js
APP.Views.Login = Backbone.View.extend({
// ... snip
success: function() {
APP.trigger('login:success'); // anything can listen to this and hook into it
}
});
// later on, in some other view
APP.Views.Subscription = Backbone.View.extend({
initialize: function() {
APP.bind('login:success', this.showSubscriptionDetails);
},
showSubscriptionDetails: function() {
// we know login was completed, show the users subscription details
this.$('.subscription-info').show();
}
});
Copy link

ghost commented Aug 6, 2014

except this is pub/sub pattern with topic-based routing ;-)

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