1 (function (){ 2 "use strict"; 3 4 /** 5 * Wrapper for localstorage data serialization 6 * 7 * @name store 8 * @namespace 9 * @memberOf $_ 10 */ 11 var store = { 12 /** 13 * Retrieves and deserializes a value from localstorage, 14 * based on the specified key 15 * 16 * @param string key 17 * @name get 18 * @memberOf $_.store 19 * @function 20 * @return object 21 */ 22 get: function (key) 23 { 24 return JSON.parse(localStorage.getItem(key)); 25 }, 26 /** 27 * Puts a value into localstorage at the specified key, 28 * and JSON-encodes the value if not a string 29 * 30 * @param string key 31 * @param mixed value 32 * @name set 33 * @memberOf $_.store 34 * @function 35 * @return void 36 */ 37 set: function (key, value) 38 { 39 if (typeof value !== "string") 40 { 41 value = JSON.stringify(value); 42 } 43 localStorage.setItem(key, value); 44 }, 45 /** 46 * Removes the specified item from localstorage 47 * 48 * @param string key 49 * @name remove 50 * @memberOf $_.store 51 * @function 52 * @return void 53 */ 54 remove: function (key) 55 { 56 localStorage.removeItem(key); 57 }, 58 /** 59 * Returns an array of all the values in localstorage 60 * in their raw form 61 * 62 * @name getAll 63 * @member of $_.store 64 * @function 65 * @return object 66 */ 67 getAll: function () 68 { 69 var i, 70 len, 71 data; 72 len = localStorage.length; 73 data = {}; 74 75 for (i = 0; i < len; i++) 76 { 77 var name = localStorage.key(i); 78 var value = localStorage.getItem(name); 79 data[name] = value; 80 } 81 82 return data; 83 } 84 }; 85 86 $_.ext('store', store); 87 }());