Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#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 dante)

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)

NodeList-data.patch (8.4 KB) - added by dante 9 years ago.

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by dante

Attachment: NodeList-data.patch added

comment:1 Changed 9 years ago by dante

worth pointing out, lots of the tests pass in DOH because it seems arrays are cast when tested with doh.is? eg:

dojo.query("#fooli").data("bar", "baz"); 
var info = dojo.query("#fooli").data("bar");
// in non-magical case, return is ['baz'], magical is: 'baz'
doh.is("baz", info); // passes in magical case because ['baz'].toString() == 'baz'

threw me for a loop when going back and forth on if it should be magical or not.

comment:2 Changed 9 years ago by dante

Description: modified (diff)

comment:3 Changed 9 years ago by James Burke

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 dante

Resolution: fixed
Status: newclosed

(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

comment:5 Changed 7 years ago by bill

In [29416]:

don't call doh.t() with a string, it tries to eval it, making a spurious test failure about a syntax error, refs #11637

Note: See TracTickets for help on using tickets.