Falcon.SugarApi/JS/StaticData.js

34 lines
901 B
JavaScript

//存储静态数据
function createSetStaticData() {
if (window.sessionStorage) {
return (k, v) => window.sessionStorage.setItem(k, v);
} else if ($ && $.cookie) {
return (k, v) => $.cookie(k, v);
} else {
throw new Error("没有找到可以支持的存储方法!");
}
}
var setStaticData = createSetStaticData();
//读取存储的静态数据
function CreateGetStaticData() {
if (window.sessionStorage) {
return (k) => window.sessionStorage.getItem(k);
} else if ($ && $.cookie) {
return (k) => $.cookie(k);
} else {
throw new Error("没有找到可以支持的存储方法!");
}
}
var getStaticData = CreateGetStaticData();
//静态数据存取类
class StaticData {
constructor(k, v) {
this.key = k;
this.value = v;
if (v) {
setStaticData(k, v);
} else {
this.value = getStaticData(k);
}
}
}