简体   繁体   English

Dojo Toolkit中的define参数

[英]Arguments in define in Dojo Toolkit

I am new to dojo. 我是新来的道场。 I have defined a widget and if looks like this: 我已经定义了一个小部件,如果看起来像这样:

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

I don't know if this is how it should be. 我不知道这是怎么回事。 Is it ok that I have so long list of requirements or do I need to require them inside the callback. 我可以列出这么长的要求列表还是可以的,是否需要在回调内要求它们?

That's not a particularly long list of modules. 那不是特别长的模块列表。 Dojo breaks its functionality down into quite small modules, so it's pretty usual to be fetching quite a few of them. Dojo将其功能分解为非常小的模块,因此获取其中的很多模块是很正常的。

You definitely don't want to be requiring them in the callback. 您绝对不希望在回调中要求它们。 define is basically the same as require , but used at the top of a module for declaring the result of an asynchronous module with dependencies. define基本与require相同,但是在模块顶部用于声明具有依赖项的异步模块的结果。

One thing worth noting is that it's probably worth injecting class dependencies to parameters with the same, or similar names: it makes the resultant declare call simpler, eg 值得注意的一件事是,可能值得将类依赖项注入具有相同或相似名称的参数:这使得结果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
    };
});

You may also wish to group your dependencies together into logical groups, eg put your base classes together, helper modules together. 您可能还希望将依赖项分组为逻辑组,例如,将基类,帮助程序模块放在一起。 You'll also be wanting to declare any other modules mentioned in your template, at the end of the module list, and you don't necessarily need to declare those as callback parameters if you're not using them in your code, eg used/by/template1 and used/by/template2 here: 您还需要想申报模板中提到的任何其他模块,在该模块列表的末尾 ,你不一定需要,如果你不是在你的代码中使用它们宣布这些回调参数,例如used/by/template1和在此处used/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
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM