Created
February 4, 2011 17:19
-
-
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
This file contains hidden or 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
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