代码寻找模块在错误的地方 [英] Code looking for modules in the wrong place

查看:119
本文介绍了代码寻找模块在错误的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用build.dojotoolkit.org(我的第一次尝试)创建了一个多层构建,它有3个层:dojo.js,dojox.js,dijit.js。每个js文件都被上传到自己的文件夹(dojo,dojox,dijit)中。

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

当我运行代码时,我希望它可以查看dijit.js来获取像dijit.form.TextBox这样的表单模块。但是它试图加载dijit / form / TextBox.js,当然最终会出现404错误。

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.

我做错了什么?

如果有帮助,文件在这里:
http://usermanagedsolutions.com/Demos/Pages

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

推荐答案

手动将每个图层包含在脚本标签上

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" />

这将提供您在构建中定义的所有模块。当您需要文本框时,Dojo会看到它具有代码,不会进行XHR调用。

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.

即使您没有使用个人的意图文件,您可能也想把它们放在服务器上。这样,如果有人忘记将该文件添加到构建中,则会产生一个xhr请求,而不是javascript错误。

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

当您按照上述方式添加图层时,您不会加载包含构建的所有模块 - 您是只需使定义函数可用,而不必进行xhr请求。

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.

如果您查看从构建输出的js文件,该文件包含模块的映射一个函数的路径,当被调用时将定义模块。

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.

所以当你写下面的代码

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

AMD将首先确定 dijit / form / 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.

如果模块尚未定义,则AMD将查看其缓存以查看定义代码是否可用。当您添加脚本文件时,您将提供一个定义函数的缓存。 AMD找到定义模块的代码。它调用这个define函数,结果是传递给回调的对象。

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.

如果模块已经有'code> dijit / form / TextBox t已经被定义,AMD在其缓存中没有找到定义函数,那么AMD将把XHR请求返回到服务器来尝试定位特定的模块代码。 XHR调用的结果应该提供定义函数。 AMD将调用该函数并将结果作为对象传入回调。再次,后续要求 dijit / form / TextBox 也将使用此对象。

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.

Dojo构建提供能力1)缩小代码,2)将其组合成需要从服务器请求的较少文件。

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允许您编写可以运行在环境(使用内置文件或单个文件),而无需进行修改。

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

这篇关于代码寻找模块在错误的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆