使用dojo同步加载模块时,依赖性未得到解决

时间:2014-05-12 16:22:01

标签: javascript asynchronous dojo amd synchronous

我正在尝试异步和同步使用相同的AMD模块(外部工具要求),但在同步模式下依赖库似乎不可用。

异步工作正常,问题是在同步模式下加载时,我收到错误:

  

ReferenceError:未定义OpenLayers [var styleMap = new OpenLayers.StyleMap({]

但是Firebug告诉我在mymodule.js之前加载了OpenLayers.js(因为我在require()中指定了顺序)

为什么OpenLayers库未解析,如何构建它以在异步和同步模式下工作?

ASYNC

<script type="text/javascript">
    //Dojo configuration
        var dojoConfig = {
            baseUrl: "/project/js/",
            tlmSiblingOfDojo: false,
            async: true,
            packages: [
                { name: "dojo",  location: "../lib/dojo/dojo" },
                { name: "openlayers", location: "../lib/openlayers", main: "OpenLayers" },
            ],
        };
    </script>

<script type="text/javascript" src="lib/dojo/dojo/dojo.js"></script>

<script>
        require([
            "mymodule",
            'dojo/domReady!'
        ], function(mymodule){

            //Initialize
            mymodule.hello();

        });
</script>

同步

<script type="text/javascript">
    //Dojo configuration
        var dojoConfig = {
            baseUrl: "/project/js/",
            tlmSiblingOfDojo: false,
            async: false,
            packages: [
                { name: "dojo",  location: "../lib/dojo/dojo" },
                { name: "openlayers", location: "../lib/openlayers", main: "OpenLayers" },
            ],
        };
    </script>

<script type="text/javascript" src="lib/dojo/dojo/dojo.js"></script>

<script>
        require([
            "openlayers",
            "mymodule",
            'dojo/domReady!'
        ], function(openlayers, mymodule){

            //Initialize
            mymodule.hello();

        });
</script>

Mymodule中

define([
    "openlayers",
], function(){

//Styles for features
var styleMap = new OpenLayers.StyleMap({
        "default": new OpenLayers.Style({
              strokeColor: "steelblue",
        }),
        "select": new OpenLayers.Style({
              strokeColor: "blue",
        })
});

function hello(){
    console.log('mymodule.hello');
}

return {
    hello: hello,
};
});

0 个答案:

没有答案