-
-
Save pollen8/1f109ab32e55027c69f6 to your computer and use it in GitHub Desktop.
map loading callback queue
This file contains 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
Fabrik.cbQueue = {'google': []}; | |
Fabrik.loadGoogleMap = function (s, cb) { | |
var src = 'http://maps.googleapis.com/maps/api/js?sensor=' + s + '&callback=Fabrik.mapCb'; | |
// Have we previously started to load the Googlemaps script? | |
var gmapScripts = $A(document.scripts).filter(function (f) { | |
return f.src === src; | |
}); | |
if (gmapScripts.length === 0) { | |
// Not yet loaded so create a script dom node and inject it into the page. | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.src = src; | |
document.body.appendChild(script); | |
// Store the callback into the cbQueue, which will be processed after gmaps is loaded. | |
Fabrik.cbQueue.google.push(cb); | |
} else { | |
// We've already added the Google maps js script to the document | |
if (Fabrik.googleMap) { | |
window[cb](); | |
// $$$ hugh - need to fire these by hand, otherwise when re-using a map object, like | |
// opening a popup edit for the second time, the map JS will never get these events. | |
//window.fireEvent('google.map.loaded'); | |
//window.fireEvent('google.radius.loaded'); | |
} else { | |
// We've started to load the Google Map code but the callback has not been fired. | |
// Cache the call back (it will be fired when Fabrik.mapCb is run. | |
Fabrik.cbQueue.google.push(cb); | |
} | |
} | |
}; | |
/** | |
* Called once the google maps script has loaded, will run through any queued callback methods and | |
* fire them. | |
*/ | |
Fabrik.mapCb = function () { | |
Fabrik.googleMap = true; | |
for (var i = 0; i < Fabrik.cbQueue.google.length; i ++) { | |
window[Fabrik.cbQueue.google[i]](); | |
} | |
Fabrik.cbQueue.google = []; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment