获取使用almond.js的RequireJS异步插件

时间:2012-12-16 05:48:30

标签: javascript google-maps requirejs almond

我阅读了以下有关如何获取Google地图的article以及gmaps.js与RequireJS合作的信息。但是,当我构建我的项目时,RequireJS与Almond交换。在上面的文章中,它指出Almond不能使用RequireJS异步插件。如果没有async插件,则不会加载Google的依赖项,gmaps.js会抛出错误。

有没有办法解决这个问题,仍然在使用Almond而不是RequireJS的项目中加载Google地图代码?

2 个答案:

答案 0 :(得分:1)

是的,我也找到了这个。它说,动态库无法加载。我想你必须在本地下载它。

答案 1 :(得分:0)

Almond.js can't handle with asynchronous plugins。您可以使用jQuery.Deferred加载库。

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"
});

使用$ .Deferred和Maplace查看this example

相关问题