Skip to content

Instantly share code, notes, and snippets.

@taktran
Last active December 16, 2015 06:09
Show Gist options
  • Save taktran/5389668 to your computer and use it in GitHub Desktop.
Save taktran/5389668 to your computer and use it in GitHub Desktop.
Load google maps asynchonously and return a function to wrap around a callback function for when google maps finishes loading. Based on http://blog.pixelingene.com/2011/10/using-jquery-dot-deferred-and-requirejs-to-lazy-load-google-maps-api/. Also see http://stackoverflow.com/q/12648598/111884
/*global $:false, window:false */
/**
* Load google maps asynchonously and return a function
* to wrap around a callback function for when google
* maps finishes loading.
*
* Note: need to load jQuery 1.5+ before this module
* is loaded.
*
* Usage:
*
* define(["gmapsDone"], function(gmapsDone) {
* function load() {
* // Do something
* }
* gmapsDone(load);
* });
*
* @return {Function} function to wrap a callback
* function for when google maps finishes loading
*/
window._mapsLoaded = $.Deferred();
window.gmapsLoaded = function() {
delete window.gmapsLoaded;
_mapsLoaded.resolve();
};
define(["http://maps.google.com/maps/api/js?v=3&sensor=false&callback=gmapsLoaded"], function(gmaps) {
"use strict";
return window._mapsLoaded.done;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment