How do I create unique IDs in a Dojo widget template?

徘徊边缘 提交于 2020-01-10 18:20:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11182103/how-do-i-create-unique-ids-in-a-dojo-widget-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!