如何在Dojo小部件模板中创建唯一的ID? [英] How do I create unique IDs in a Dojo widget template?
问题描述
/ code>标签的code>属性需要一个HTML ID值,但Dojo小部件不应该包含ID,以防在同一页上创建多个实例。 p>
所以,有没有人有任何建议如何解决这些矛盾的需求?
开箱即用,如果配置参数在构建时不存在,dijit注册表将如何设置WidgetID(this.id):
code> constructor:function(args){args = args || {};
this.id = args.id || dijit.registry.getUniqueId(this.declaredClass)
}
模板与字符串替换,因此,如果您的课堂中有财产,请说 foo ,将此 模板的方式如下所示:
templateString ='< div class =$ {foo}>';
在你的情况下,模板中的某个地方有一个label-> input对,它就像这个
< div><! - domNode - >
< table>
< td>< label for =$ {id} -edit-title>标题< / label>< / td>
< td>< input id =$ {id} -edit-titletype =text/>< / td>
< / table>
< / div>
所以
有点过时了,这是一个非常好的开始的地方:
http:/ /dojotoolkit.org/documentation/tutorials/1.6/templated/
继续阅读 dojo.Stateful
get / set mechanism
最后转到 dijit._WidgetsInTemplateMixin
。
I have a Dojo widget that I'm writing that adds a label and an input box to the user's page.
The for
attribute of a label
requires an HTML ID value, but a Dojo widget should not contain IDs in case multiple instances are created on the same page.
So, does anyone have any suggestions on how to work around these conflicting needs?
Out the box, this is how the dijit registry sets WidgetID (this.id) if the configuration parameter is not present while constructing:
constructor: function(args) { args=args || {};
this.id = args.id || dijit.registry.getUniqueId(this.declaredClass)
}
Templates works with string replacements, so if you have a property in your class, say foo, the way to place this into the template is as such:
templateString = '<div class="${foo}">';
In your case, where somewhere in the template you have a label->input pair, it goes like this
<div><!--domNode-->
<table>
<td><label for="${id}-edit-title">Title</label></td>
<td><input id="${id}-edit-title" type="text" /></td>
</table>
</div>
So
Allthough it is a little bit outdated for time being, this is a very good place to start: http://dojotoolkit.org/documentation/tutorials/1.6/templated/
Continue reading on the dojo.Stateful
get/set mechanism
Finally turn to dijit._WidgetsInTemplateMixin
.
这篇关于如何在Dojo小部件模板中创建唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!