如何在 Dojo 小部件模板中创建唯一 ID? [英] How do I create unique IDs in a Dojo widget template?

查看:16
本文介绍了如何在 Dojo 小部件模板中创建唯一 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Dojo 小部件,它向用户页面添加标签和输入框.

labelfor 属性需要 HTML ID 值,但 Dojo 小部件不应包含 ID,以防在同一页面上创建多个实例.

那么,有人对如何解决这些相互冲突的需求有任何建议吗?

解决方案

如果在构建时配置参数不存在,dijit 注册表就是这样设置 WidgetID (this.id) 的:

构造函数:function(args) { args=args ||{};this.id = args.id ||dijit.registry.getUniqueId(this.declaredClass)}

模板适用于字符串替换,所以如果你的类中有一个属性,比如 foo,那么将这个 放入模板的方法如下:

templateString = '<div class="${foo}">';

在您的情况下,模板中某处有一个标签->输入对,它是这样的

<div><!--domNode--><桌子><td><label for="${id}-edit-title">标题</label></td><td><input id="${id}-edit-title" type="text"/></td></表></div>

所以

虽然暂时有点过时,但这是一个非常好的起点:http://dojotoolkit.org/documentation/tutorials/1.6/templated/p>

继续阅读 dojo.Stateful 获取/设置机制

最后转向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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆