2012-12-16 18 views
9

Ho letto il seguente article su come ottenere Google Maps e gmaps.js per lavorare con RequireJS. Tuttavia, quando costruisco il mio progetto, RequireJS è scambiato con Almond. Nell'articolo sopra, afferma che Almond non funzionerà con il plugin Async RequireJS. Senza il plugin asincrono, le dipendenze di Google non vengono caricate e gmaps.js genera un errore.Ottenere RequireJS plugin asincrono che funziona con almond.js

C'è un modo per aggirare il problema e caricare ancora il codice di Google Maps in un progetto che utilizza Almond anziché RequireJS?

risposta

1

Sì, sto trovando anche questo. Le librerie dinamiche non possono essere caricate dice. Immagino che dovrai scaricarlo localmente.

0

Almond.js can't handle with asynchronous plugins. È possibile utilizzare jQuery.Deferred per caricare le librerie.

var googleMapsLoader = function(func, options) { 
    var defaults = { 
     "sensor" : "false", 
     "v"  : "3", 
     "key"  : "", 
     "language" : "pt", 
     "region" : "br", 
     "libraries": "" 
    }; 

    $.when($.ajax({ 
     type: "GET", 
     dataType: "script", 
     data: $.extend({}, defaults, options), 
     url: "https://maps.google.com/maps/api/js", 
     crossDomain: true 
    })).then(function() { 
     func(); 
    }); 
}; 

/* 
* Loading Google Maps API with $.Deferred. 
*/ 
googleMapsLoader(function() { 
    // You may call your code here. 
}, { 
    "libraries" : "geometry,places", 
    "v"   : "3.7" 
}); 

Guarda this example utilizzando $ .Deferred e Maplace.

Problemi correlati