Andrew Bialecki![]()
UPDATE: Deprecated: See the Node module and class which now support everything you need natively.
Sometimes it's useful to store arbitrary data with a particular node or object. If your class uses the Attribute utility, you're in luck and you're job is done. However, the Node class doesn't (yet) use the Attribute utility, so this port of the jQuery data storage API allows you to associate data with a particular Node instance. In fact, you're not limited to a Y.Node instances -- any object will do.
For Y.Node and Y.NodeList instances, two methods -- data() and removeData() -- are added to their prototypes and chainable so you can easily store and retrieve data you want to associate with those elements.
Here's a quick snippet of what you can do with the data storage module covering use with Node instances, NodeList instances and arbitrary JavaScript objects.
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>YUI({
//Last Gallery Build of this module
gallery: 'gallery-2010.03.16-20'
}).use('gallery-data-storage', function(Y) {
// Use it with a single element...
var foo = Y.one("#foo").data("bar", "baz");
foo.data(); // { bar: "baz" }
foo.data("bar"); // "baz"
foo.removeData();
foo.data("bar") // undefined
// Use it with a list of elements...
var lis = Y.all("li").data("type", "you are a li"),
first = lis.item(0).data("type", "you're the leader li");
first.data(); // { type: "you're the leader li" }
lis.item(1).data("type"); // "you are a li"
// Use it with any old object...
var obj = {},
DS = Y.DataStorage;
DS.data(obj, "favoriteTeam", "redSox");
DS.data(obj, "favoriteColor", "orange");
DS.data(obj); // { favoriteTeam: "redSox", favoriteColor: "orange" }
DS.removeData(obj, "favoriteTeam");
DS.data(obj, "favoriteColor"); // "orange"
});No forum posts for this module.
© 2006-2011 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info