Login Register

New attribute mapping feature for Dijit (ticket #3058)

In revision [10527] the attributemap branch has merged into trunk. Now all widgets have a property called attributeMap that is responsible for listing attributes which Dijit should copy over from its source when creating a new widget.

Dijit should be a better HTML citizen now and "inherit" standard HTML attributes where appropriate in markup, like ID, NAME, TITLE, TABINDEX, etc. CLASS definitions and inline STYLE elements will be merged in with templated widgets as appropriate. Prior to this, widgets would drop these attributes on markup or constructors unless explicitly copied over by the template or widget implementation.

What does this mean to most developers?

Most developers shouldn't notice the change. It should help make the Dijit more consistent, fix some bugs, and make some templates more concise. This change ought to be well-encapsulated to all except those who need to design their own custom widgets.

What do you have to know if you're writing custom widgets?

All definitions of "attributeMap", including the base _Widget class, inherit from the parent using mixins. Look at _Widget for documentation and the various Dijit implementations for other examples. "attributeMap" is a hash containing a list of attributes and the attach points where they should be copied to, with domNode as the default. If your widget defines new attributes which should be carried over from the parser or constructor input, add them to the map. If any of the attach points change or are no longer available, make those adjustments as well or the widget may fail to instantiate.

Does that fix...

...the onclick attributes of form fields?

haven't put the event handlers in yet

...but I think we could. I'll take a look.

probably not for 1.0

generalizing events is possible but seems to involve a bit more work. Plus, we already have a mechanism to do this called dojoAttachEvent. It's something we should look at, but not something I think I want to do for 1.0. Aside from simplifying things, would this solve a problem?

There is a bug filed already...

...on the checkbox "onclick" handler, and I think someone picked it up for 1.1. I'm the one who filed the bug, but put a workaround in my function I called in the onclick event. I just thought that if the attribute mapping feature inherited the base attributes of the object, then it would fix the event handlers. Thanks for looking. Great work, btw. With all my testing on this stuff, I'm helping y'all with the bug testing.

can we define our own custom html attributes?

I want to use custom attributes in a dijit.MenuItem

Example:

  • Folders, Not Items
  • when this widget is parsed, I want it to pick up the 'usage' attribute so I can later pick it up and evaluate the json object. I'll use this to conditionally enable and disable menuItems per context.

    What I am unaware of is how to make this a reality. I've tried this exact scenario, but the attributeMap is turning up empty. can anyone help me out?

    thanks!

    You can define your own

    You can define your own widget attributes (which can be used from HTML markup) by simply declaring a property on your widget prototype in dojo.declare. It's the existence of the property that makes the parser pick up the attribute and assign it to the Object. You can create your own widget class or inherit from MenuItem to do this. You can also mix-in attributes to the widget object (would be for all instances of that object type) but that's sort of cheating...

    attributeMap is for mapping attributes which are already declared onto certain parts of a template (for templated widgets), which probably isn't what you want here.