Skip to content

Instantly share code, notes, and snippets.

@i-like-robots
Created April 30, 2013 15:01
Show Gist options
  • Save i-like-robots/5489285 to your computer and use it in GitHub Desktop.
Save i-like-robots/5489285 to your computer and use it in GitHub Desktop.
An interface for asynchronously loading the Google Maps API and queuing callbacks.
define(function() {
return {
/**
* Queue
* @type {Array}
*/
queue: [],
/**
* Init
*/
init: function() {
var self = this;
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&callback=mapsQueueCallback';
document.head.appendChild(script);
window.mapsQueueCallback = function() {
self.execute();
};
},
/**
* Add
* @param {Function} callback
*/
add: function(callback) {
if ( ! window.mapsQueueCallback) {
this.init();
}
this.queue.push(callback);
},
/**
* Execute
*/
execute: function() {
while (this.queue.length) {
this.queue.pop().call(window);
}
if (window.mapsQueueCallback) {
delete window.mapsQueueCallback;
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment