dojo.registerModulePath and baseUrl

Here is some interesting points about baseUrl & registerModulePath.
Usually if you use dojo.js from local / same domain and if your module is placed inside the same folder then just go straight and use dojo.require(ModuleName.JavaScriptFileName).
Otherwise if you are using CDN for dojo.js and also you need to use your own module then go with baseUrl and dojo.registerModulePath.

Here is my understanding on baseUrl. Look into the image below to understand the folder structure that I have taken as example.
Folder Structure of custom module myChart under js folder

Also note that the html file is placed in the same level of the js folder inside which myChart module is kept.

Now to mention the path for my module, I have to first mention the base path inside which my module is placed. So I have to write like this.
djConfig="baseUrl: './js'"


So now I have mentioned that the base path for my module is the js folder. Now I have to register the path of my module to access my js files inside it. We need to use dojo.registerModulePath("myChart", "./myChart"). While writing this, I tested the working of registerModulePath and found in IE 9, FF 5, Chrome 12.0.742, while loading the page using http://localhost/dojoDemo/myPieV2.html , that registerModulePath is not required, since my module is kept in the first level inside js folder which I have defined using baseUrl.


I used wamp server to host it in local. That is how I used this path. If you are not used wamp before just download and install it in your machine. Place your projects inside the "www" folder. Remember to start the server. You can see it in the system tray and ensure that it is running. Otherwise just do a single click on it and click on "Start All Services" menu.


Alright, let us place my module inside some other folder moving it to the second level under js folder like shown in this image below.
myChart folder moved into "insidejs" folder
Understand here that the folder myChart is now kept inside a folder named "insidejs" under js folder. So now my baseUrl remains the same as I mentioned earlier. But we need to change the registerModulePath value as given below.
dojo.registerModulePath("myChart", "./insidejs/myChart");


I hope you got some idea on baseUrl and registerModulePath. I found that it is always better to use some servers to host your files locally and run it in browser like Chrome using http to get proper output.

For example and code on this topic refer to dojo.declare and Pie chart topics in my blog. Please write your questions in comment, so that it will be helpful for other also.



4 comments:

  1. This is great post on registerModulePath example.. I tried for me it works on FF and IE9 after allowing ActiveX control.. but no luck with Chrome.. Do I have to change my browser settings if any? Please help..

    ReplyDelete
  2. Happy to see you here. Did you tried to access the file through server like WAMP? I haven't changed any configuration in my browser. It was not working for me in chrome, when I directly accessed from local drive. And it worked fine after running it using server. It may be the security policy of chrome. Please check and update.
    Thank you.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete

Please post your comments to help you and others better.