用于替换Ext.state.CookieProvider的另一个StateProvider

由于CookieProvider的长度4k限制,每次都提交cookie的内容
实现了一个基于firefox的gloabalStorage,基于ie的userdata的状态管理器,代码如下:

Ext.state.SessionStorageStateProvider = function(config){
Ext.state.SessionStorageStateProvider.superclass.constructor.call(this);
try{
if(Ext.isIE)
{
this.storage = new ewp.util.IESessionStorage("ewpstate");
}else if(window.globalStorage)
{
this.storage = window.globalStorage[window.location.hostname];
}
}catch(e){
}
};

Ext.extend(Ext.state.SessionStorageStateProvider, Ext.state.Provider, {
get : function(name, defaultValue){
if(!this.storage)
{
return defaultValue;
}
try{
var value = this.storage.getItem("ys-"+name)
return value == "undefined" ? defaultValue : this.decodeValue(value);
}catch(e){
return defaultValue;
}
},
// private
set : function(name, value){
if(!this.storage)
{
return;
}
if(typeof value == "undefined" || value === null){
this.clear(name);
return;
}
try{
this.storage.setItem("ys-"+name, this.encodeValue(value));
Ext.state.SessionStorageStateProvider.superclass.set.call(this, name, value);
}catch(e){
}
},

// private
clear : function(name){
if(!this.storage)
{
return;
}
try{
this.storage.removeItem(name)
Ext.state.SessionStorageStateProvider.superclass.clear.call(this, name);
}catch(e){
}
}
});

ewp.util.IESessionStorage = function(fileName){
this.fileName = fileName;
this.ele = document.documentElement;
this.ele.addBehavior("#default#userdata");
this.ele.load(fileName);
}
ewp.util.IESessionStorage.prototype = {
setItem:function(key, value){
this.ele.setAttribute(key, value);
this.ele.save(this.fileName);
},
getItem:function(key){
return this.ele.getAttribute(key);
},
removeItem:function(key){
this.ele.removeAttribute(key);
this.ele.save(this.fileName);
},
deleteSelf:function(){
this.ele.expires = new Date(315532799000).toUTCString();
this.ele.save(this.fileName);
}
}


由于不清楚当长度达到限制或者存储所依赖的文件发生变动,系统将出现如何的状况,所以就统一的把异常都吃掉了。
参考内容:
ie下使用userdata的例子:[url]http://www.blogjava.net/emu/articles/39485.html[/url]
[url]http://msdn.microsoft.com/en-us/library/ms531424(VS.85).aspx[/url]
[url]https://developer.mozilla.org/cn/DOM/Storage[/url]
[url]http://en.wikipedia.org/wiki/HTTP_cookie[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值