Skip to content

Instantly share code, notes, and snippets.

@vitch
Created February 4, 2011 17:19
Show Gist options
  • Save vitch/811401 to your computer and use it in GitHub Desktop.
Save vitch/811401 to your computer and use it in GitHub Desktop.
Example of a backbone controller with methods which wait for initialisation before running
MapPageController = Backbone.Controller.extend(
{
initialized: false,
routes: {
'' : 'index',
'/trip/:id' : 'trip'
},
initialize: function()
{
var c = this;
Trips = new TripList();
Trips.fetch({
error: function(collection, xhr)
{
alert('Error loading data. Sorry!');
},
success: function(collection, xhr)
{
// Do stuff with the loaded data then...
c.initialized = true;
c.trigger('initialized');
}
});
},
index: function()
{
},
trip: function(tripId)
{
var trip,
args = arguments;
if (this.initialized) {
// Do stuff which relies on Trips being populated
} else {
this.bind(
'initialized',
function()
{
this.unbind('initialized');
args.callee.apply(this, args);
}
);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment