这样的dojo标记中的id和data-dojo-id有什么区别:
<button id="save" data-dojo-type="dijit/form/Button" type="button" data-dojo-attach-event="onClick:save">Save</button>
我尝试引用此按钮以更改其标签:
var myButton = dijit.byId(“save”);
这样我就可以更改按钮标签
myButton.set(“label”,“添加新”);
如果我使用id =“save”,它可以工作。如果我仅使用data-dojo-id =“save”,则无法使用。
我对Dojo相当陌生,因此非常感谢您可以向我提出的解释或教程!
最佳答案
data-dojo-id
将小部件分配给全局 namespace ,即window
对象:
<button data-dojo-id="save" data-dojo-type="dijit/form/Button">Save</button>
因此您可以直接访问该按钮:
save.set("label", "Add New");
在jsFiddle上查看操作上的区别:http://jsfiddle.net/phusick/7yV56/
编辑:回答您的问题。我根本不使用
data-dojo-id
。它会污染全局 namespace ,这与AMD的做法正好相反。无论如何,您仍然可以使用widgets.save
和widgets.rename
这样的方法来最大程度地减少污染:<button data-dojo-id="widgets.save" data-dojo-type="dijit/form/Button">Save</button>
<button data-dojo-id="widgets.rename" data-dojo-type="dijit/form/Button">Rename</button>
IMO,
data-dojo-id
用于progressive enhancement,不适用于成熟的应用程序。data-dojo-id
只是将实例分配给变量,因此,对于具有相同data-dojo-id
的多个dijit,变量将指向最后分配的变量(即,它不是数组)。您可以避免大量使用
registry.byId
编写方法来根据需要获取小部件。最好的开始方法是 dijit/registy.findWidgets(rootNode, skipNode)
。另请注意, dojo/parser.parse(rootNode, options)
返回实例化对象的数组,或更准确地说,它返回:Returns a blended object that is an array of the instantiated objects, but also can include a promise that is resolved with the instantiated objects. This is done for backwards compatibility. If the parser auto-requires modules, it will always behave in a promise fashion and
parser.parse().then(function(instances){...})
should be used.
我用来将ContentPane的dijits分配给它的
widgets
属性的方法的一个示例,该属性是一个对象:_attachTemplateWidgets: function(widgets) {
widgets = widgets || this.getChildren();
for(var each = 0; each < widgets.length; each++) {
var widget = widgets[each];
var attachPoint = widget.params.dojoAttachPoint;
if(attachPoint) {
this.widget[attachPoint] = widget;
}
var children = widget.getChildren();
if(children.length > 0) {
this._attachTemplateWidgets(children);
}
}
}
我把整个类(class)放在这里:https://gist.github.com/3754324。我使用此
app.ui._Pane
而不是dijit/layout/ContentPane
。
关于dojo - id和data-dojo-id之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469140/