#11637 closed enhancement (fixed)
[patch][cla] Add NodeList-data to core
Reported by: | dante | Owned by: | dante |
---|---|---|---|
Priority: | high | Milestone: | 1.6 |
Component: | Core | Version: | 1.5 |
Keywords: | Cc: | ||
Blocked By: | Blocking: |
Description (last modified by )
This is probably the most often requested bit of functionality for users when they are migrating from jQuery, and we don't have a simple solution. dojo.data is "too much", and telling folks to write their own plugin for it is silly. I wrote one, it lives in plugd. It differs slightly from $.data in that you can pass a hash as a setter, and get an entire data object back by passing no parameters.
Exposes a single-node function dojo._data (which probably needs renamed, but is private ... )
One thing to note, and I'm note sure if I like it: Instead of following the other NodeList functions that return a map, I found the single-node case to be the most relevant. eg: dojo.query("#id").data("bar") ... most other NodeList functions return an array from .map, but I made this one determine if it is a single item list and return !this[0]. Feels like magic, but the alternative is cumbersome:
// setter: dojo.query("#bar").data("bar", { a:"b" }); // getter, traditional: if("b" == dojo.query("#bar").data("bar")[0].a){} // getter, magical: if("b" == dojo.query("#bar").data("bar").a){}
The magical version being not so magical and failing violently when your list is .length > 1 and you aren't expecting it to be.
Thoughts? The inline docs reflect the non-magical state, and will be updated to reflect whatever is decided.
Attachments (1)
Change History (6)
Changed 9 years ago by
Attachment: | NodeList-data.patch added |
---|
comment:1 Changed 9 years ago by
comment:2 Changed 9 years ago by
Description: | modified (diff) |
---|
comment:3 Changed 9 years ago by
Looks good to me. From the meeting it sounds like it would not have the magical behavior of if only one node, return data object, but always return an array. I would like that to change in the future though, perhaps in a 2.0 thing, but understand keeping consistency with the other NodeList? get operations.
comment:4 Changed 9 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [22788]) fixes #11637 - adding NodeList?-data to core. docs are ready, no live examples until after commit http://docs.dojocampus.org/dojo/NodeList-data
worth pointing out, lots of the tests pass in DOH because it seems arrays are cast when tested with doh.is? eg:
threw me for a loop when going back and forth on if it should be magical or not.