论坛首页 Web前端技术论坛

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

浏览 2599 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-07   最后修改:2009-04-07
由于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的例子:http://www.blogjava.net/emu/articles/39485.html
http://msdn.microsoft.com/en-us/library/ms531424(VS.85).aspx
https://developer.mozilla.org/cn/DOM/Storage
http://en.wikipedia.org/wiki/HTTP_cookie
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics