I'm in the midst of creating my first backbone app using Node/express and Mongodb. The app is an online food menu and ordering system that collects all the data from mongo on the first page load and pushes it into a Backbone collection. I'm using the initialize function in the Backbone Router to fetch() the data and put it into a collection. My problem is that the index Router is loading before the Initialize function has finished. I have confirmed that the initialize function runs correctly because I can access the collection in the browser console.
I may be going about this the wrong way but here is a simplified version of my router:
GDB.Router = Backbone.Router.extend ({
routes: {
'': 'index',
'drinks': 'drinks'
},
meals: {},
initialize: function() {
meals = new GDB.Collections.Meals
meals.fetch();
},
index: function() {
var mealsView = new GDB.Views.Meals ({collection: meals});
$('#GDBContainer').append(mealsView.render().el);
},
drinks: function() {
var drinksView = new GDB.Views.Meals ({collection: meals.byCategory('drinks')});
$('#GDBContainer').append(drinksView.render().el);
}
});