public
Last active — forked from ZenCocoon/element_storage.js

  • Download Gist
element_storage.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/**
* Inspired from Mootools 1.2 Element.Storage (http://blog.mootools.net/2008/1/22/Element_Storage)
*
* License:
* MIT-style license.
*
* Author:
* Sebastien Grosjean (http://zencocoon.com)
*
* Tested under FF 2, FF3, Safari 3, Opera 9.25, IE6, IE7, Camino 1.6
**/
/**
* Extend Prototype to add UID
*
**/
Object.extend(Prototype, {UID: 1});
/**
* Storage handler
*
**/
Element.Storage = {
get: function(uid) {
return (this[uid] || (this[uid] = {}));
},
init: function(item) {
return (item.uid || (item.uid = Prototype.UID++));
}
}
/**
* Element.retrieve(@element, property, default) => value
*
* Retrieve from the external object Element.Storage and scoping the element, the value defined by property
* If no value exist default is stored and returned
*
**/
Element.Methods.retrieve = function(element, property, dflt) {
if (!(element = $(element))) return;
if (element.uid == undefined) Element.Storage.init(element);
var storage = Element.Storage.get(element.uid);
var prop = storage[property];
if (dflt != undefined && prop == undefined)
prop = storage[property] = dflt;
return prop;
};
/**
* Element.store(@element, property, value) => @element
*
* Store a property / value pair attached to an element in the external object Element.Storage
*
**/
Element.Methods.store = function(element, property, value) {
if (!(element = $(element))) return;
if (element.uid == undefined) Element.Storage.init(element);
Element.Storage.get(element.uid)[property] = value;
return element;
};
Element.addMethods();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.