How do I create unique IDs in a Dojo widget templa

2020-02-28 03:56发布

问题:

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.



标签: dojo widget