A couple of things.

I've got a version of Element.getData published as part of my Behavior work:

https://github.com/anutron/behavior/blob/master/Source/Element.Data.js

It's not that complicated, though it includes a get/setJSONData which allows
you to use JSON in the values.

More interesting though is the BehaviorAPI:

https://github.com/anutron/behavior/blob/master/Source/BehaviorAPI.js

The docs are more readable:

https://github.com/anutron/behavior/blob/master/Docs/BehaviorAPI.md

Nutshell: you give it a prefix - a namespace for a group of data attributes
- and you get a bunch of tools to work with them.

<div id="div" data-foo-char="a" data-foo-number="1" data-foo-array="[1,2,3]"
data-foo-options="'char2':'b', 'arr2':[4,5,6], 'bool': false">...</div>

var api = new BehaviorAPI(document.id('div'), 'foo');

api.getAs(Number, 'char'); //throws error
api.requireAs(Number, 'char'); //throws error
api.require('char', 'array', 'baz'); //throws error; baz not defined

api.setDefault({
  char: 'x', //does not change because char is defined as a
  arr3: [6,7,8]
});

api.get('char'); //a
api.getAs(Number, 'number'); //1 << integer
api.getAs(Number, 'notDefined'); //undefined
api.getAs(Array, 'arr2'); [4,5,6]);

api.getAs({
  'char': String,
  'char2': String,
  'arr': Array,
  'arr2': Array
}); //returns { char: 'a', char2: 'b', arr: [1,2,3], arr2: [4,5,6] }

etc.


On Tue, Jun 28, 2011 at 9:53 AM, Ryan Florence <[email protected]> wrote:

> jQuery's combination of element storage + data attributes is rather
> elegant.
>
> <div id=el data-foo=bar></div>
>
> $('#el').data('foo') //> 'bar';
>
> I've been thinking of implementing the same behavior in
> Element.prototype.retrieve, though get('data-foo') is completely adequate.
>
> These attributes are incredibly useful in client-side templating and
> declarative JavaScript architectures.
>
> As for the tangent on this thread ... If you're concerned about validating
> XHTML, I feel bad for you.
>
>
> On Jun 28, 2011, at 10:00 AM, Arieh Glazer wrote:
>
> So what? What does valid mean? Does it break any browser - no. Does it
> break the semantics of the page? no. Does it decrease your ability to use
> the validator to identify syntax errors? maybe - but then again - why not
> simply switch to h5? the browsers couldn't care less about your doctype (as
> documented at several different resources)
>
> On Tue, Jun 28, 2011 at 6:40 PM, Steve Onnis <[email protected]>wrote:
>
>> It might work but it is not valid XHTML pre HTML5****
>>
>> ** **
>>
>> *From:* Arian Stolwijk [mailto:[email protected]]
>> *Sent:* Wednesday, 29 June 2011 12:50 AM
>>
>> *To:* [email protected]
>> *Subject:* Re: [Moo] Re: Custom data attribute****
>>
>> ** **
>>
>> this does even work in IE6.****
>>
>> On Tue, Jun 28, 2011 at 4:12 PM, Steve Onnis <[email protected]>
>> wrote:****
>>
>> The comment was in reference to pre HTML5 as it was asked if mootools
>> would support custom attributes for non HTML5 browsers****
>>
>>
>> -----Original Message-----
>> From: Tim Wienk [mailto:[email protected]]
>> Sent: Tuesday, 28 June 2011 11:14 PM
>> To: [email protected]
>> Subject: Re: [Moo] Re: Custom data attribute****
>>
>> On Tue, Jun 28, 2011 at 15:03, Steve Onnis <[email protected]>
>> wrote:
>> > Keep in mind though that doing that sort of thing will invalidate your
>> XHTML
>> > unless you generate your own dtd schema and implement that
>>
>>
>> http://dev.w3.org/html5/spec/elements.html#embedding-custom-non-visible-data-with-the-data-attributes
>>
>> --
>> Tim Wienk, Software Developer, MooTools Developer
>> E. [email protected] | W. http://tim.wienk.name****
>>
>> ** **
>>
>
>
>
> --
> Arieh Glazer
> אריה גלזר
> 052-5348-561
> http://www.arieh.co.il
> http://www.link-wd.co.il
>
>
>

Reply via email to