Using directive to store data

2 views
Skip to first unread message

Adam Fisk

unread,
Jan 14, 2009, 12:44:10 AM1/14/09
to PURE Unobtrusive Rendering Engine for HTML
I'd like to use jquery.data to store single items of JSON in the HTML
element they're corresponding with, and I'm wondering how the
directives can handle that.

I want something like:

var directives = {
"div" : function (arg) {
$(div).data(json, arg.item);
...default behavior
}
};

...where div is my top-level element.

So this would be like a decorator around the default behavior. So I'm
wondering:

1) Can I call the default behavior from a directive, or trigger it
somehow?
2) Can I access that default div that would be returned to call the
jquery data(...,...) method on it?

Thanks so much -- fantastic project.

-Adam

Mic Cvilic

unread,
Jan 14, 2009, 4:38:03 AM1/14/09
to Pure-Unobtrusive...@googlegroups.com
When your function directive is executed, the element does not exist
yet in the DOM.
So jquery.data can't be used in a directive.

You can add an attributes and its value with a directive:
'div[data-id]':function(arg){
return arg.item.firstName;}

It is ok if you want to store a single item. But it will be converted
as a string.
The result will be like <div data-id="Adam">
This strangely non namespaced "data-" comes from HTML5 specs.

For more serious storage you can build your cache system like
jquery.data(...)
Then set a data-id attribute as above and set/get in a global object
hash table(like jQuery.cache) your data with an id.
That's what I did for the app we are building.

cheers,

Adam Fisk

unread,
Jan 14, 2009, 11:40:21 AM1/14/09
to PURE Unobtrusive Rendering Engine for HTML
Thanks Mic-- I haven't explored jquery.cache, but that sounds very
promising. I'll have a look. You probably saw this, but jquery 1.3
went final late last night.

Thanks again.

-Adam
Reply all
Reply to author
Forward
0 new messages