Sorry, the title is a little confusing.
My backbone router has the following structure:
var AppRouter = Backbone.Router.extend({
routes: {
'notes': 'showNotes',
'news': 'showNews'
},
showNews: function() {
// make view object and render
},
showNotes: function() {
// make view object and render
},
initialize: function() {
this.user.fetch({success: function(){
// callback function
}});
}
});
The issue I am having is that i need to pass the user into the views, so i need each render to only run if the success callback runs inside of initialize. Basically I don’t want initialize to finish until the callback is called. I cant figure out how I could achieve this.
Thanks
Router#initialize, by default, is an empty function. At the point it’s run, the routes have already been handed off toHistory, and you’re probably past any “clean” way to prevent them.If you really need to ensure that your user is fetched before the router starts rendering, you could achieve that by fetching the
userbefore the history begins, something like this:But it may also make sense to have the view respond to the user being fetched, rather than ensuring that it’s loaded beforehand. The view might simply render nothing until the user loads, or it might show a “loading” graphic, etc. In which case you’d just: