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"}} } });
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年