最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding a package to dojo config at runtime - Stack Overflow

programmeradmin1浏览0评论

Is there a way I can add a new package to dojo config? I know I can do this: Add packages when dojo.js loads.

<script src='dojo_1.7.2/dojo/dojo.js' 
    data-dojo-config="async:true,isDebug:true,parseOnLoad:false,
      packages:[{name:'project1',location:'../../js/proj1'},
        {name:'mon',location:'../../mon'}]"></script>

I want to be able to add new packages at runtime.

dojo.registerModulePath did do this job prior to dojo1.6 (i think) but its now deprecated in 1.7

I am using dojo 1.7.2.

Thanks.

Is there a way I can add a new package to dojo config? I know I can do this: Add packages when dojo.js loads.

<script src='dojo_1.7.2/dojo/dojo.js' 
    data-dojo-config="async:true,isDebug:true,parseOnLoad:false,
      packages:[{name:'project1',location:'../../js/proj1'},
        {name:'mon',location:'../../mon'}]"></script>

I want to be able to add new packages at runtime.

dojo.registerModulePath did do this job prior to dojo1.6 (i think) but its now deprecated in 1.7

I am using dojo 1.7.2.

Thanks.

Share Improve this question asked Nov 14, 2012 at 16:07 Sheldon FernandesSheldon Fernandes 1,3091 gold badge11 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can add extra packages after load by calling require with a config object.

Eg:

require({
    packages: [
        {"name": "myLib", "location": "release/myLib"}
    ]
});

This will however, create another instance of Dojo, according to the documentation (dojo/_base/config). Also, this is version 1.8 code; I don't think it works with 1.7.

I thought it might possible to push an extra object to dojoConfig or require.rawConfig but these are not picked-up by the loader. It appears that config cannot be changed after load.

You can pass a config object to require, so:

Eg.

dojoConfig.packages.push({"name": "myLib", "location": "release/myLib"});

require(dojoConfig, [...moduleIds...], function(...arguments...) {
});

This will work for the individual require but will not modify the global config (and hence will not work in define() or subsequent calls to require()). Again, I'm using 1.8 here but I assume it works in 1.7.

There may be another simpler way of making this work that someone else as found?

The solution by Stephen Simpson didn't seem to work right for me with dojo v1.13. It ignored the given location and was still trying to load the files relative to the default basePath despite of the project path starting it with a /. I got errors in the console, too.

But the documentation also mentions the paths parameter which worked for me. In your case:

require({paths:{"project1": "../../js/proj1", …}});

It probably worked for you because you're using a relative path and I don't.

It used to be dojo.registerModulePath("myModule", "/path/goes/here");.

发布评论

评论列表(0)

  1. 暂无评论