• mass Framework data模块


    mass Framework的内存对象缓存系统,它会在目标对象或目标对象对应的代理对象添加一个uniqueID ,用于关链相应的缓存体。通过dom.data把数据储存到相应的缓存体中去。

    //==================================================
    // 数据缓存模块
    //==================================================
    (function(global,DOC){
        var dom = global[DOC.URL.replace(/(#.+|\W)/g,'')];
        dom.define("data", "lang", function(){
            dom.log("已加载data模块");
            var remitter = /object|function/
            dom.mix(dom,{
                memcache:{},
                /*
                 * @param {Any} target
                 * @param {String|Object} name
                 * @param {Any} data
                 **/
                data : function( target, name, data, pvt ) {
                    if(target && remitter.test(typeof target)){
                        var id = target.uniqueNumber || (target.uniqueNumber=++dom["@uuid"]);
                        if(name === "@uuid"){
                            return id;
                        }
                        var memcache = target.nodeType === 1 ? dom.memcache: target;
                        var table = memcache[ "@data_"+id ] || (memcache[ "@data_"+id ] = {});
                        if ( !pvt ) {
                            table = table.data || (table.data = {});
                        }
                        var getByName = typeof name === "string";
                        if ( name && typeof name == "object" ) {
                            dom.mix(table, name);
                        }else if(getByName && data !== void 0){
                            table[ name ] = data;
                        }
                        return getByName ? table[ name ] : table;
                    }
                },
                _data:function(target,name,data){
                    return dom.data(target, name, data, true)
                },
                /*
                 * @param {Any} target
                 * @param {String|Opt} name
                 **/
                removeData : function(target, name, pvt){
                    if(target && remitter.test(typeof target)){
                        var id = target.uniqueNumber;
                        if (  !id ) {
                            return;
                        }
                        var memcache = target.nodeType === 1  ? dom.memcache : target;
                        var table = memcache["@data_"+id], clear = 1, ret = typeof name == "string" ;
                        if ( table && ret ) {
                            if(!pvt){
                                table = table.data
                            }
                            if(table){
                                ret = table[ name ];
                                delete table[ name ];
                            }
                            var cache = memcache["@data_"+id];
                                loop:
                                for(var key in cache){
                                    if(key == "data"){
                                        for(var i in cache.data){
                                            clear = 0;
                                            break loop;
                                        }
                                    }else{
                                        clear = 0;
                                        break loop;
                                    }
                                }
                        }
                        if(clear){
                            delete memcache["@data_"+id];
                        }
                        return ret;
                    }
                },
                /*
                 *把对象dest的数据缓存体合并到src的数据缓存体上
                 * @param {Any} target
                 * @param {String|Opt} name
                 **/
                mergeData:function(clone, src){
                      var srcData = dom._data(src), cloneData = dom._data(clone);
                    if(srcData && cloneData){
                        dom.Object.merge.call(cloneData, srcData);
                    }
                }
            });
        });
    })(this,this.document);
    //2011.9.27 uniqueID改为uniqueNumber 简化data与removeData
    //2011.9.28 添加dom._data处理内部数据
    

    示例

    
          dom.require("data",function(){
            var a = {};
            //写入数揣
            dom.data(a,"name","司徒正美");
            //读取数据
            dom.log(a);//{ uniqueNumber:2, "@data_2":{data:{name:"司徒正美"}} }
            var b = {};
            dom.data(b,"sex","man");
            //合并数据
            dom.mergeData(b,a);
            dom.log(b);//{ uniqueNumber:3, "@data_3":{data:{name:"司徒正美",sex:"man"}} }
            //删除数据
            dom.removeData(b,"name");
            dom.log(b);//{ uniqueNumber:3, "@data_3":{data:{sex:"man"}} }
            dom.data(b,"inner","innerHTML",true);
            dom.log(b);//{ uniqueNumber:3, "@data_3":{inner:"innerHTML",data:{sex:"man"}} }
          });
    
    
  • 相关阅读:
    剑指offer(第2版)刷题 Python版汇总
    git学习笔记
    Python中的lambda、map和filter
    算法题 22 折纸问题 (牛客网,今日头条)
    算法题 21 findNSum (好未来,LeetCode,牛客网)
    算法题 20 或与加
    Python的内存管理机制
    【算法题12 解码方法decode way】
    理解循环神经网络的来龙去脉
    机器学习资源个人汇总
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2111964.html
Copyright © 2020-2023  润新知