Skip to content

Instantly share code, notes, and snippets.

@raglan-road
Created October 26, 2012 14:19
Show Gist options
  • Save raglan-road/3959086 to your computer and use it in GitHub Desktop.
Save raglan-road/3959086 to your computer and use it in GitHub Desktop.
promises thing
initMap = function(){
var self = this;
return $.Deferred(function(dfd){
if(self.mapLoaded){
// Map is loaded y'all.
dfd.resolve();
}
else{
$.ajax( // load your thing)
.done(function(){
// Map loaded correctly? Set our mapLoaded flag and resolve our promise
self.mapLoaded = true;
dfd.resolve();
});
}
}).promise();
}
// ... and then
$.when(this.initMap())
.done(function(){
// Maps are loaded! This will either execute after your Ajax request
// if the map isn't loaded or immediately if the map is already loaded.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment