Added store module, ecapsulated QS module

This commit is contained in:
Timothy Warren 2011-06-14 12:20:53 -04:00
parent 34a660651c
commit 6ecc805fd2

53
kis.js
View File

@ -14,6 +14,7 @@
$_ = {}; $_ = {};
window.$_ = window.$_ || $_; window.$_ = window.$_ || $_;
window.$_.hb = (history.pushState) ? false : true;
$ = function(a) $ = function(a)
{ {
@ -102,8 +103,9 @@
* *
* Object for encoding and decoding querystrings and hashbang strings * Object for encoding and decoding querystrings and hashbang strings
*/ */
$_.qs = { (function(){
_parse: function(hb) var qs = {
parse: function(hb)
{ {
hb = hb || $_.hb; hb = hb || $_.hb;
var h, i, hString, pairs, pLen, data, y; var h, i, hString, pairs, pLen, data, y;
@ -145,7 +147,7 @@
set: function(key, value, hb) set: function(key, value, hb)
{ {
hb = hb || $_.hb; hb = hb || $_.hb;
var pairs = this._parse(hb); var pairs = this.parse(hb);
if(key !== undefined && value !== undefined) if(key !== undefined && value !== undefined)
{ {
@ -175,9 +177,52 @@
get: function(key, hb) get: function(key, hb)
{ {
hb = hb || $_.hb; hb = hb || $_.hb;
var pairs = this._parse(hb); var pairs = this.parse(hb);
return (pairs[key]) ? pairs[key] : ''; return (pairs[key]) ? pairs[key] : '';
} }
}; };
window.$_.qs = qs;
}());
/**
* Store object
*
* Wrapper for localstorage data serialization
*/
(function(){
var store = {
get: function(key)
{
return JSON.parse(localStorage.getItem(key));
},
set: function(key, value)
{
if(typeof value === "object")
{
value = JSON.stringify(value);
}
ls.setItem(key, value);
},
getAll: function()
{
var i, len, data;
len = localStorage.length;
data = {};
for(i=0;i<len;i++)
{
var name = localStorage.key(i);
var value = localStorage.getTime(name);
data[name] = value;
}
return data;
}
};
window.$_.store = store;
}());
})(); })();