我目前已經在「localStorage」中添加了一些方法。RequireJS中的「localstorage」原型
/**
*
* MOVED TO: https://github.com/iFind/html5MultidimensionalStorage
*
* This methods extends the default HTML5 Storage object and add support
* to set and get multidimensional data
*
* @example Storage.setObj('users.albums.sexPistols',"blah");
* @example Storage.setObj('users.albums.sexPistols',{ sid : "My Way", nancy : "Bitch" });
* @example Storage.setObj('users.albums.sexPistols.sid',"Other songs");
*
* @example Storage.getObj('users');
* @example Storage.getObj('users.albums');
* @example Storage.getObj('users.albums.sexPistols');
* @example Storage.getObj('users.albums.sexPistols.sid');
* @example Storage.getObj('users.albums.sexPistols.nancy');
*
* This is just a prototype and is not recommended to use at production apps
* USE AT YOUR OWN RISK
*
* @author Klederson Bueno <[email protected]>
* @author Gabor Zsoter <[email protected]>
*/
//Add Storage support for objects
Storage.prototype.__walker = function(path,o) {
//Validate if path is an object otherwise returns false
if(typeof path !== "object")
return undefined;
if(path.length === 0){
return o;
}
for(var i in path){
var prop = path[i];
//Check if path step exists
if(o.hasOwnProperty(prop)){
var val = o[prop];
if(typeof val == 'object'){
path.splice(0,1);
return this.__walker(path,val);
} else {
return val;
}
}
}
};
Storage.prototype.setObj = function(key, value) {
var key = encodeURIComponent(key);
var path = key.split('.');
//First level is always the localStorage key pair item
var _key = path[0];
var os = this.getItem(_key) !== null ? JSON.parse(this.getItem(_key)) : null; //general storage key pair element
path.splice(0,1);
if(os === null) {
os = {};
this.setItem(_key,JSON.stringify(os));
}
var innerWalker = function(path,o) {
//Validate if path is an object otherwise returns false
if(typeof path !== "object")
return undefined;
if(path.length == 1) {
o[path[0]] = value;
return o;
} else if(path.length === 0) {
os = value;
return os;
}
var val = null;
for(var i in path){
var prop = path[i];
//Check if path step exists
if(o.hasOwnProperty(prop)) {
val = o[prop];
if(typeof val == 'object'){
path.splice(0,1);
return innerWalker(path,val);
}
} else {
//create depth
o[prop] = {};
val = o[prop];
path.splice(0,1);
return innerWalker(path,val);
}
}
};
innerWalker(path,os);
this.setItem(_key,JSON.stringify(os));
};
Storage.prototype.getObj = function(key) {
var key = encodeURIComponent(key);
key = key.split('.');
//First level is always the localStorage key pair item
var _key = key[0];
var o = this.getItem(_key) ? JSON.parse(this.getItem(_key)) : null;
if(o === null)
return undefined;
key.splice(0,1);
return this.__walker(key,o);
};
在另一類,我做的:
define(['jquery', '_Errors'], function($, Errors) {
[...]
localStorage.getObj('blabla');
[...]
});
之前,我遷移到RequireJS我只是曾在一個名爲functions.js我所有的功能/原型 - 這是仍然是可能的?還是我需要指定我將在每個文件中使用的所有功能?
謝謝 - 我結束了使用選項一,因爲我已經有一個名爲「global.js」的領域與很多一般的東西。 – FooBar 2014-09-01 08:02:37