Skip to content

Instantly share code, notes, and snippets.

@NouranMahmoud
Forked from MattSurabian/google-maps-loader.js
Last active August 29, 2015 14:19
Show Gist options
  • Save NouranMahmoud/c6433348dc619f8f5129 to your computer and use it in GitHub Desktop.
Save NouranMahmoud/c6433348dc619f8f5129 to your computer and use it in GitHub Desktop.
/**
* GoogleMapsAPI Loader Module
*
* Returns a promise that resolves with the google.maps object when all of the google maps api loading process is complete
*
* Example Usage:
*
* define([ 'app/lib/google-maps-loader' ], function(GoogleMapsLoader){
* GoogleMapsLoader.done(function(GoogleMaps){
* // your google maps code here!
* var geocoder = new GoogleMaps.Geocoder();
* }).fail(function(){
* console.error("ERROR: Google maps library failed to load");
* });
* });
*
* -OR-
*
* define([ 'app/lib/google-maps-loader' ], function(GoogleMapsLoader){
* GoogleMapsLoader.done(function(){
* // your google maps code here!
* var geocoder = new google.maps.Geocoder();
* }).fail(function(){
* console.error("ERROR: Google maps library failed to load");
* });
* });
*
*/
var google_maps_loaded_def = null;
define(['jquery'],function($) {
if(!google_maps_loaded_def) {
google_maps_loaded_def = $.Deferred();
window.google_maps_loaded = function() {
google_maps_loaded_def.resolve(google.maps);
}
require(['http://maps.googleapis.com/maps/api/js?sensor=true&callback=google_maps_loaded'],function(){},function(err) {
google_maps_loaded_def.reject();
//throw err; // maybe freak out a little?
});
}
return google_maps_loaded_def.promise();
});
@NouranMahmoud
Copy link
Author

CoffeeScript version

google_maps_loaded_def = null

define ['jquery'], ($) -> 

  if not google_maps_loaded_def

    google_maps_loaded_def = $.Deferred()

    window.google_maps_loaded = ->
      google_maps_loaded_def.resolve(google.maps)    

    require ['https://maps.googleapis.com/maps/api/js?sensor=true&callback=google_maps_loaded'],
      () -> {}, 
      (err) ->
        google_maps_loaded_def.reject()


  google_maps_loaded_def.promise()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment