Added Element#store and Element#retrieve for safe, hash-backed storage of element metadata (no memory leaks). Also added Element#getStorage for working with the element's storage hash directly. Hat tip: Mootools.
This commit is contained in:
parent
99f74b526a
commit
3977e66796
|
@ -1,3 +1,5 @@
|
|||
* Added Element#store and Element#retrieve for safe, hash-backed storage of element metadata (no memory leaks). Also added Element#getStorage for working with the element's storage hash directly. Hat tip: Mootools. (ZenCocoon, Andrew Dupont)
|
||||
|
||||
* Fix issue where certain versions of Safari treat class names case-insensitively in Selector/$$ queries. (Andrew Dupont, kangax, Brice)
|
||||
|
||||
* Fix issue where Function#argumentNames returned incorrect results in IE when comments were intermixed with argument names. (Christophe Porteneuve, T.J. Crowder)
|
||||
|
|
|
@ -1205,3 +1205,41 @@ document.viewport = {
|
|||
window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
|
||||
}
|
||||
};
|
||||
|
||||
Element.Storage = {
|
||||
UID: 1
|
||||
};
|
||||
|
||||
Element.addMethods({
|
||||
getStorage: function(element) {
|
||||
if (!(element = $(element))) return;
|
||||
|
||||
if (Object.isUndefined(element._prototypeUID))
|
||||
element._prototypeUID = [Element.Storage.UID++];
|
||||
|
||||
var uid = element._prototypeUID[0];
|
||||
|
||||
if (!Element.Storage[uid])
|
||||
Element.Storage[uid] = $H();
|
||||
|
||||
return Element.Storage[uid];
|
||||
},
|
||||
|
||||
store: function(element, key, value) {
|
||||
if (!(element = $(element))) return;
|
||||
element.getStorage().set(key, value);
|
||||
},
|
||||
|
||||
retrieve: function(element, key, defaultValue) {
|
||||
if (!(element = $(element))) return;
|
||||
|
||||
var hash = element.getStorage(), value = hash.get(key);
|
||||
|
||||
if (Object.isUndefined(value)) {
|
||||
hash.set(key, defaultValue);
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1365,6 +1365,23 @@ new Test.Unit.Runner({
|
|||
constants.each(function(pair) {
|
||||
this.assertEqual(Node[pair.key], pair.value);
|
||||
}, this);
|
||||
},
|
||||
|
||||
testElementStorage: function() {
|
||||
var element = $('test-empty');
|
||||
element.store('foo', 'bar');
|
||||
this.assertEqual("bar", element.retrieve("foo"), "Setting and reading a property");
|
||||
element.store('foo', 'thud');
|
||||
this.assertEqual("thud", element.retrieve("foo"), "Re-setting and reading property");
|
||||
|
||||
element.store('bar', 'narf');
|
||||
this.assertEnumEqual($w('foo bar'), element.getStorage().keys(), "Getting the storage hash");
|
||||
element.getStorage().unset('bar');
|
||||
this.assertEnumEqual($w('foo'), element.getStorage().keys(), "Getting the storage hash after unsetting a key");
|
||||
|
||||
|
||||
var clonedElement = $('test-empty').cloneNode(false);
|
||||
this.assert(!('_prototypeUID' in clonedElement), "Cloning a node should not confuse the storage engine");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue