Opened 10 years ago

Closed 8 years ago

#9910 closed enhancement (fixed)

dijit._Widget.attributeMap should support Widgets, not just domNodes

Reported by: Phil DeJarnett Owned by: bill
Priority: high Milestone: 1.8
Component: Dijit Version: 1.3.2
Keywords: dijit._Widget.attr dojo.attr attributeMap Cc:
Blocked By: Blocking:

Description (last modified by bill)

If I want to make a simple, compound widget (say, combining a Slider with a TextBox), there is no easy way to map properties from the parent _Widget to child widgets. (The child widgets in this case are created using a template with widgetsInTemplate set to true.)

If you attempt to use attributeMap, the widget being set will throw deprecated warnings about setAttribute (because dojo.attr() is calling setAttribute).

Possibly the best solution that would entail the least amount of code would be to have dojo.attr() use attr() if that method already exists on the node being set. I'm not sure how to make a proper diff, but I see this being inserted in dojo.attr:

if(node.attr) {
    return args == 3 ? node.attr(name, value) : node.attr(name);
}
name = _fixAttrName(name);
if(args == 3){ //setter
...

I acknowledge that changes to core dojo methods are probably risky and avoided whenever possible, so as an alternative, this kind of code could be added to _Widget instead.

---

Example of usage: In my example above, my compound slider widget puts a NumberTextBox at the end of the Slider. This textbox can be typed into, or the slider can be adjusted normally. However, I want to be able to map basic attributes like name, required, and promptMessage to the TextBox (which is what is submitted).

Currently, the only way to get this to work with a templated widget is to manually add getters and setters for every mapped property. This not only adds bulk to the class, but is tedious for more than a few properties.

Change History (3)

comment:1 Changed 10 years ago by bill

Description: modified (diff)
Milestone: tbdfuture

Hi Phil,

I can see the reason for asking for this.

About the implementation, it seems like calling attr() on the widgets might be limited because certain attributes (on certain widgets) can only be specified at initialization time. But I guess it would still be better than nothing.

Maybe you could file a CLA and then help to write some test cases to add into _Templated-widgetsInTemplate.html? That's half the battle of adding new features.

comment:2 Changed 8 years ago by bill

Milestone: future1.8
Owner: set to bill
Status: newassigned

Note that attributeMap has been replaced by custom setters, for example:

_setFooAttr: "focusNode"

will apply the "foo" attribute to this.focusNode.

I agree, it would be a nice enhancement for that to work even if this.focusNode is a widget, rather than a DOMNode, and it's a trivial change to _attrToDom() to make that work:

if(mapNode.set){
    mapNode.set(attrName, value);
}else{
    domAttr.set(mapNode, attrName, value);
}

At one point I thought this worked already but from looking at the code I guess it doesn't.

I will add it for 1.8, or maybe even 1.7.

comment:3 Changed 8 years ago by bill

Resolution: fixed
Status: assignedclosed

In [27077]:

Allow custom setters with string values (ex: _setFooAttr: "foo") to apply to widgets in addition to DOMNodes. Fixes #9910 !strict.

Note: See TracTickets for help on using tickets.