Dojo Toolkit中define中的参数



我是dojo的新手。我已经定义了一个小部件,如果看起来像这样:

define(["dojo/_base/declare", "dijit/_Widget", "dijit/_TemplatedMixin", "dojo/text!Widgets/Templates/Screen.html", "./Node", "dojo/_base/array", "dojo/dom-geometry", "dojo/dom-style"],
function (declare, widget, templated, template, node, array, domGeometry, domStyle) { 
...

我不知道是否应该这样。我有这么长的需求列表可以吗?还是需要在回调中要求它们。

这不是一个特别长的模块列表。Dojo将其功能分解为相当小的模块,因此通常会获取相当多的模块。

你肯定不想在回调中要求它们。definerequire基本相同,但用于模块顶部,用于声明具有依赖关系的异步模块的结果。

值得注意的一点是,向具有相同或相似名称的参数注入类依赖关系可能是值得的:它使生成的declare调用更简单,例如

define(["dojo/_base/declare", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/text!mytemplate.html"], function(declare, _WidgetBase, _TemplatedMixin, template) {
return declare("my.Widget", [_WidgetBase, _TemplatedMixin], {
templateString: template
};
});

您可能还希望将依赖项组合到逻辑组中,例如,将基类、辅助模块组合在一起。您还需要在模块列表的声明模板中提到的任何其他模块,如果您在代码中没有使用它们,则不一定需要将它们声明为回调参数,例如used/by/template1used/by/template2

define(["dojo/_base/declare", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/text!mytemplate.html", "used/by/template1", "used/by/template2"], function(declare, _WidgetBase, _TemplatedMixin, template) {
return declare("my.Widget", [_WidgetBase, _TemplatedMixin], {
templateString: template
};
});

最新更新