Skip to content

Instantly share code, notes, and snippets.

@savetheclocktower
Forked from ZenCocoon/element_storage.js
Created January 28, 2009 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savetheclocktower/53924 to your computer and use it in GitHub Desktop.
Save savetheclocktower/53924 to your computer and use it in GitHub Desktop.
/**
* 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment