Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

widget

dojo

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?

like image 514
Dancrumb Avatar asked Jun 24 '12 23:06

Dancrumb


1 Answers

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.

like image 184
mschr Avatar answered Sep 27 '22 22:09

mschr