Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a package to dojo config at runtime

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:'common',location:'../../common'}]"></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.

like image 806
Sheldon Fernandes Avatar asked Nov 14 '12 16:11

Sheldon Fernandes


2 Answers

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?

like image 67
Stephen Simpson Avatar answered Oct 06 '22 02:10

Stephen Simpson


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

like image 22
DanMan Avatar answered Oct 06 '22 01:10

DanMan