如何通过CDN使用Dojo加载自定义AMD模块?

时间:2012-01-03 00:01:53

标签: javascript dojo amd

我正在使用谷歌的CDN,并尝试使用他们的加载器加载我自己的AMD模块。我知道我做错了什么但是我被卡住了。有什么想法吗?

    <script src="https://ajax.googleapis.com/ajax/libs/dojo/1.7.0/dojo/dojo.js" 
            type="text/javascript" data-dojo-config="async:true,parseOnLoad:true"></script>
    <script type="text/javascript">
        require(["dojo/_base/kernel", "dojo/_base/loader", "dojo/parser"], function(dojo){
            dojo.registerModulePath("pgGallery", "http://127.0.0.1:8080/js");
        });
        require(["pgGallery/Message"], function(m){
            m.success("foo");
        });
    </script>

http://127.0.0.1:8080/js/Message.js是模块的位置。

2 个答案:

答案 0 :(得分:11)

我想出了如何在这里做到:http://dojotoolkit.org/reference-guide/quickstart/cross-domain.html在“使用带有本地模块的CDN”下。

页面中的示例:

<script type="text/javascript">
    var dojoConfig = {
        async: true,
        packages: [
            {
                name: "my",
                location: "/absolute/path/to/local/modules"
            }
        ]
    };
</script>

<!-- Bootstrap Dojo From Google's CDN -->
<!-- removing the protocol from src url auto detects if current page is served via http or https and also loads the dojo resources from matching protocol -->
<script
    type="text/javascript"
    src="//ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js">
</script>

<script type="text/javascript">
    require(["my/FooModule"], function(FooModule){
        // ...
    });
</script>

答案 1 :(得分:0)

我应该使用的另一个选项,我正在使用的是这个。注意“.js”。不知何故,装载机以不同的方式对待它。

require(["pgGallery/Message.js"], function(m){
        m.success("foo");
    });

请参阅:http://dojotoolkit.org/reference-guide/1.8/loader/amd.html

  

如果moduleId以协议(例如,“http:”)或正斜杠开头,或以“.js”后缀结尾,则假定请求是针对任意一块JavaScript,而不是模块。< / p>

我不知道这是否是最佳做法。

相关问题