store.js 4.7 KB
Newer Older
C
Catouse 已提交
1
/* Store */
2
+function(window, $)
C
Catouse 已提交
3 4 5 6 7
{
    "use strict";

    var lsName = 'localStorage';
    var storage = window[lsName],
8 9
        old = window['store'],
        pageName = 'page_' + window.location.pathname;
C
Catouse 已提交
10 11 12 13 14 15 16

    /* The Store object */
    var Store = function()
    {
        this.slience = true;
        this.disabled = (lsName in window) && window[lsName] && window[lsName]['setItem'];
        this.storage = storage;
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
        var self = this;

        this.page = this.get(pageName, {});
    };

    /* Save page data */
    Store.prototype.savePage = function()
    {
        if($.isEmptyObject(this.page))
        {
            this.remove(pageName);
        }
        else
        {
            this.set(pageName, this.page);
        }
    };

    /* Clear page data */
    Store.prototype.clearPage = function()
    {
        this.page = {};
        this.savePage();
    };

    /* Get page data */
    Store.prototype.getPage = function(key, defaultValue)
    {
        var val = this.page[key];
        return (defaultValue !== undefined && val === null) ? defaultValue : val;
    };

    /* Set page data */
    Store.prototype.setPage = function(objOrKey, val)
    {
        if($.isPlanObject(objOrKey))
        {
            $.extend(true, this.page, objOrKey);
        }
        else
        {
            this.page[this.serialize(objOrKey)] = val;
        }
        this.savePage();
C
Catouse 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    };

    /* Check disabled status */
    Store.prototype.check = function()
    {
        if(this.disabled)
        {
            if(!this.slience) throw new Error('Browser not support localStorage or disabled status been set true.');
        }
        return !this.disabled;
    };

    /* Get length */
    Store.prototype.length = function()
    {
        if(this.check())
        {
            return storage.length;
        }
    };

    /* Remove item with browser localstorage native method */
    Store.prototype.removeItem = function(key)
    {
        storage.removeItem(key);
    };

    /* Remove item with browser localstorage native method, same as removeItem */
    Store.prototype.remove = function(key)
    {
        this.removeItem(key);
    };

    /* Get item value with browser localstorage native method, and without deserialize */
    Store.prototype.getItem = function(key)
    {
        return storage.getItem(key);
    };

    /* Get item value and deserialize it, if value is null and defaultValue been given then return defaultValue */
    Store.prototype.get = function(key, defaultValue)
    {
        var val = this.deserialize(this.getItem(key));
        return (defaultValue !== undefined && val === null) ? defaultValue : val;
    };

    /* Get item value by key and deserialize it */
    Store.prototype.key = function(key)
    {
        return this.deserialize(storage.key(key));
    };

    /* Set item value with browser localstorage native method, and without serialize filter */
    Store.prototype.setItem = function(key, val)
    {
        storage.setItem(key, val);
    };

    /* Set item value, serialize it if the given value is not an string */
    Store.prototype.set = function(key, val)
    {
        if(val === undefined) return this.remove(key);
        this.setItem(key, this.serialize(val));
    };

    /* Clear all items with browser localstorage native method */
    Store.prototype.clear = function()
    {
        storage.clear(key);
    };

    /* Iterate all items with callback */
    Store.prototype.forEach = function(callback)
    {
        for(var i = 0; i < storage.length; i++)
        {
            var key = storage.key(i);
            callback(key, store.get(key));
        }
    };

    /* Get all items and set value in an object. */
    Store.prototype.getAll = function(callback)
    {
        var all = {};
        this.forEach(function(key, val)
        {
            all[key] = val;
        });

        return all;
    };

    store.getAll = function() {
      var ret = {}
      store.forEach(function(key, val) {
        ret[key] = val
      })
      return ret
    }

    /* Serialize value with JSON.stringify */
    Store.prototype.serialize = function(value)
    {
        if(typeof value === 'string') return value;
        return JSON.stringify(value);
    };

    /* Deserialize value, with JSON.parse if the given value is not a string */
    Store.prototype.deserialize = function(value)
    {
        if(typeof value !== 'string') return undefined;
        try
        {
            return JSON.parse(value);
        }
        catch(e)
        {
            return value || undefined;
        }
    };

    var store = new Store();

    window.store = store;

    window.store.noConflict = function()
    {
        window.store = old;
        return store;
    }
192
}(window, jQuery);