Code looking for modules in the wrong place

一世执手 提交于 2020-01-05 03:01:06

问题


I have created a multi-layer build using build.dojotoolkit.org (my first attempt) with 3 layers: dojo.js, dojox.js, dijit.js. Each js file is uploaded in its own folder (dojo,dojox,dijit).

When I run the code, I would expect it to look in dijit.js to get the form modules like dijit.form.TextBox. But instead it tries to load dijit/form/TextBox.js and of course ends up with a 404 error.

What am I doing wrong?

The files are here if it helps: http://usermanagedsolutions.com/Demos/Pages


回答1:


Manually include each layer in a script tag on the page.

<script src="path/to/dojo.js" />
<script src="path/to/dojox.js" />
<script src="path/to/dijit.js" />

This will make available all modules that you have defined in the build. When you require the text box, Dojo will see that it has the code and will not make the XHR call.

Even though you do not have the intention of using the individual files, you may want to put them on the server as well. This way if someone forgets to add the file to the build, the penalty incurred is an xhr request, as opposed to a javascript error.


Re: AMD

When you include your layers in the manner that I described above, you are not loading all the modules that you included the build - you are just making the define functions available without having to make xhr requests.

If you look at the js file that is output from the build, the file contains a map of the module path to a function that when called will define the module.

So when you write the following code

require(["dijit/form/TextBox"], function(TextBox){
  ...
});

AMD will first determine if dijit/form/TextBox has already been defined. If so it will just take the object and execute the callback.

If the module hasn't already been defined, then AMD will look in it's cache to see if the define code is available. When you include your script files, you are providing a cache of define functions. AMD finds the code to define the module. It calls this define function and the result is the object that is passed into the callback. Subsequent requires for dijit/form/TextBox will also use this object as described above.

If the module hasn't already been defined and AMD does not find the define function in its cache, then AMD will make an XHR request back to the server to try to locate the specific module code. The result of the XHR call should provide the define function. AMD will call the function and use the result as the object to pass into the callback. Again, subsequent requires for dijit/form/TextBox will also use this object.

The Dojo build, provides the ability to 1) minify the code and 2) combine it into fewer files that need to be requested from the server.

AMD allows you to write code that can run in either environment (using built files or the individual files) without having to make modifications.



来源:https://stackoverflow.com/questions/13806747/code-looking-for-modules-in-the-wrong-place

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!