Created
October 7, 2011 13:38
-
-
Save davemo/1270297 to your computer and use it in GitHub Desktop.
Using backbone custom events and the observer pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
except this is pub/sub pattern with topic-based routing ;-)