如何知道哪个Dojo AMD要求条款没有名称? [英] How to know which Dojo AMD require clause with no name?
问题描述
例如:
对于选择器查询,我看到例如,不给dojo / NodeList-dom和dojo的名称准备好。
require([dojo / query, dojo / dom-class,dojo / on,dojo / NodeList-dom,dojo / domReady!],function(query,domClass,on){
/ pre>
它只是可选的给他们的名字?所以如果我不想要一个声明他们的名字,我应该把它们列在列表的末尾? / p>
或者我可以给他们任意的名字吗?我看到一些例子,使用名为request的变量给domReady命名。
我假设这是正确的?
require([dojo / query,dojo / dom- class,dojo / on,dojo / NodeList-dom,dojo / domReady!],function(query,domClass,on,nodeList,request){
/ pre>
这不是吗?
//没有名字对于NodeList-dom
require([dojo / q dojo / dom-class,dojo / on,dojo / NodeList-dom,dojo / domReady!],function(query,domClass,on,request){
另外,你怎么知道是否需要他们的名字,我可以不命名任何一个吗?
解决方案你可以选择你的名字,什么不是。这取决于模块的作用以及需要回调参数以便使用它。例如,
dojo / query
模块对于查询DOM节点很有用,但不能实际使用它(功能参数)。
另一方面,像
dojo / NodeList-dom
之类的模块扩展了dojo / query
模块,你不需要他们的回调来做它的东西。
小部件可以是有用的有或没有回调参数。如果您使用声明的小部件,则不需要回调,而如果以编程方式创建它们,则需要该小部件来实例化,例如:
require([dijit / form / FilteringSelect],function(FilteringSelect){
return new FilteringSelect({});
});
虽然声明性地你只需要:
require([dijit / form / FilteringSelect,dojo / parser],function(){
});
但即使您的模块没有目的一个回调参数(例如
dojo / NodeList-dom
),你仍然可以命名他们:require([dojo / NodeList-dom],function(NodeList){
});
但它们对您而言并不有用。所以你可以实际上命名所有的模块,如果你发现这更容易。
找出是否需要命名的最好方式是看看他们在参考指南中使用。如果您看到回调参数用于调用模块函数,例如:
require ([dojo / query,dojo / NodeList-dom,dojo / domReady!],function(query){
query(#myNode)forEach(function(node){
console.log(node);
});
});
我们使用
查询
回调来查询#myNode
,所以我们需要命名。但是,dojo / NodeList-dom
扩展了查询
(例如forEach )
函数),所以我们不需要命名它。
dojo / domReady!
module / plugin不一定要命名。该模块将确保仅在加载DOM时调用回调函数。
一个重要的注意事项是回调参数的顺序与模块的顺序相同。即使一个模块没有返回一个可以使用的正确的对象,它仍然值得。例如(以下将不起作用):
require([dojo / NodeList-dom ,dojo / query,dojo / domReady!],function(query){
query(#myNode)forEach(function(node){
console.log ;
});
});
许多人认为,因为
dojo / NodeList-dom
不返回一个有用的对象,他们可以这样离开,但这不是真的。查询
callback参数实际上将包含dojo / NodeList-dom
在此示例中返回的数据(因为它是第一个参数)无论数据可能是什么。
所以如果你实际上不需要模块的回调参数,最简单的方法是将它添加到最后或者您可以执行以下操作(如前所述):
require( [dojo / NodeList-dom,dojo / query,dojo / domReady!],function(NodeList,query){
query(#myNode)forEach(function(node) b $ b console.log(node);
});
});
然而,对于其他开发人员(阅读代码)这个例子可能会令人困惑,因为他们可能会认为他们可以使用
NodeList
参数。所以为了保持你的代码的可读性,我建议你在模块列表的末尾放置dojo / NodeList-dom
。
现在我们来看看我们可以回答你的代码示例:
你的第一个例子是正确的,因为我刚刚解释
dojo / NodeList-dom
和dojo / domReady!
不返回有用的对象,所以你可以把它们留下来。 / p>
你的第二个例子也是正确的。您可以选择使用这些模块来命名,即使您不使用它们的回调。
第三个例子也是正确的,然而
请求
参数将包含dojo / NodeList-dom
回调,不dojo / domReady!
回调。你可以选择你的名字。是否有效取决于您将如何处理请求
回调。我的意思是,以下示例也可以工作:require([dojo / query ,dojo / NodeList-dom,dojo / domReady!],function(NodeList,domReady,query){
NodeList(#myNode)。forEach(function(node){
console .log(node);
});
});
但是在这种情况下,
NodeList
参数包含dojo / query
,domReady
的结果包含dojo / NodeList- dom
和查询
包含dojo / domReady!
的结果。听起来很混乱,它会奏效。但是如你所见,要查询#myNode
我需要dojo / query
的结果,这是NodeList
在这种情况下。很混乱,不是吗?那你应该选择正确的名字。I have a question on Dojo declaration. I noticed in the AMD declaration for required library for dojo that some library does not have declared name in the funciton parameter. How do you know which one has no parameter?
For example:
For selector query, I saw an example which does not give name for dojo/NodeList-dom and dojo ready.
require(["dojo/query", "dojo/dom-class", "dojo/on", "dojo/NodeList-dom", "dojo/domReady!"], function(query, domClass, on) {
Is it just optional to give them names? So if I do not want a declaration for their name, I should place them in the end of the list?
Or can I give them arbitrary names? I saw some example that gives names to domReady with a variable named "request".
I am assuming that this is correct?
require(["dojo/query", "dojo/dom-class", "dojo/on", "dojo/NodeList-dom", "dojo/domReady!"], function(query, domClass, on, nodeList, request) {
While this is not?
// No name for NodeList-dom require(["dojo/query", "dojo/dom-class", "dojo/on", "dojo/NodeList-dom", "dojo/domReady!"], function(query, domClass, on, request) {
Also, how do you know if you need name for them, can I just not name any of them?
解决方案You can choose what you name and what not. It depends on what the module does and if you need the callback parameter in order to work with it. For example, the
dojo/query
module is useful for querying DOM nodes, but you can't use it without an actual reference to it (the function parameter).On the other hand, modules like
dojo/NodeList-dom
extend thedojo/query
module, you won't need their callback to do stuff with it.
Widgets can be useful with or without callback parameter. If you use widgets declarative, you don't need the callback while if you create them programmatically, you need the widget to instantiate it, for example:
require(["dijit/form/FilteringSelect"], function(FilteringSelect) { return new FilteringSelect({}); });
While declaratively you only need:
require(["dijit/form/FilteringSelect", "dojo/parser"], function() { });
But even if your module has no purpose to have a callback parameter (for example
dojo/NodeList-dom
), you can still name them anyway:require(["dojo/NodeList-dom"], function(NodeList) { });
But they won't be useful to you. So you can in fact name all modules if you find that easier.
The best way to find out if you need to name it or not, is by looking at how they're used at the reference guide. If you see that the callback parameter is used to call the module functions, for example:
require(["dojo/query", "dojo/NodeList-dom", "dojo/domReady!"], function(query) { query("#myNode").forEach(function(node) { console.log(node); }); });
We use the
query
callback to query#myNode
, so we need to name it. However, thedojo/NodeList-dom
extends thequery
(with for example theforEach()
function) so we don't need to name it.The
dojo/domReady!
module/plugin doesn't have to be named either. This module will make sure that the callback function is only called when the DOM is loaded.
One important note is that the order of the callback parameters is the same as the order of the modules. Even if a module doesn't return a proper object you can use, it still counts. For example (the following will not work):
require(["dojo/NodeList-dom", "dojo/query", "dojo/domReady!"], function(query) { query("#myNode").forEach(function(node) { console.log(node); }); });
Many people think that, because
dojo/NodeList-dom
doesn't return a useful object, they can just leave it that way, but that isn't true. Thequery
callback parameter will actually contain the data that is returned bydojo/NodeList-dom
in this example (because it's the first parameter) whatever that data may be.So if you don't actually need the callback parameter of the module, the easiest thing is to add it to the end of the module list or you could do the following (as I explained before):
require(["dojo/NodeList-dom", "dojo/query", "dojo/domReady!"], function(NodeList, query) { query("#myNode").forEach(function(node) { console.log(node); }); });
This example will work, however, for other developers (reading your code) it may be confusing because they might think they can use the
NodeList
parameter. So for the sake of keeping your code readable, I suggest you just putdojo/NodeList-dom
at the end of the module list.
Now let's see if we can answer your code examples:
Your first example is correct, as I just explained.
dojo/NodeList-dom
anddojo/domReady!
do not return useful objects, so you can leave them out.Your second example is correct as well. You can choose to name these modules anyway, even if you don't use their callback.
The third example is also correct, however the
request
parameter will contain thedojo/NodeList-dom
callback and not thedojo/domReady!
callback. You may choose how you name them. Whether it works or not depends on what you're going to do with therequest
callback. I mean, the following example will work as well:require(["dojo/query", "dojo/NodeList-dom", "dojo/domReady!"], function(NodeList, domReady, query) { NodeList("#myNode").forEach(function(node) { console.log(node); }); });
But in this case, the
NodeList
callback parameter contains the result ofdojo/query
,domReady
contains the result ofdojo/NodeList-dom
andquery
contains the result ofdojo/domReady!
. How confusing it may sound, it will work. But as you can see, to query#myNode
I need the result ofdojo/query
, which isNodeList
in this case. Confusing, isn't it? That's whŷ you should choose proper names.这篇关于如何知道哪个Dojo AMD要求条款没有名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!