Created
July 19, 2012 21:18
-
-
Save josephmisiti/3146871 to your computer and use it in GitHub Desktop.
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
// create pub-sub functionality | |
Backbone.pubSub = _.extend({}, Backbone.Events); | |
// view one needs to trigger an event in view2 | |
View1 = Backbone.View.extend({ | |
triggerView2Event : function() { | |
Backbone.pubSub.trigger('view2event', { 'some' : 'data' } ); | |
}) | |
}) | |
// view two which changes based on the view2event | |
View2 = Backbone.View.extend({ | |
initialize: function() { | |
Backbone.pubSub.on('view2event', this.onChange, this); | |
}, | |
onChange : function() { | |
// update the view | |
} | |
}) | |
Here is my case with a similar need: Backbone listenTo seemed like a solution to redirect to login page for timed out or not authenticated requests.
I added event handler to my router and made it listen to the global event such as:
Backbone.Router.extend({
onNotAuthenticated:function(errMsg){
var redirectView = new LoginView();
redirectView.displayMessage(errMsg);
this.loadView(redirectView);
},
initialize:function(){
this.listenTo(Backbone,'auth:not-authenticated',this.onNotAuthenticated);
},
.....
});
and in my jquery ajax error handler:
$(document).ajaxError(
function(event, jqxhr, settings, thrownError){
.......
if(httpErrorHeaderValue==="some-value"){
Backbone.trigger("auth:not-authenticated",errMsg);
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the tip! Helpful :)