欢迎来到天天文库
浏览记录
ID:40908280
大小:41.50 KB
页数:6页
时间:2019-08-10
《页面js数据缓存》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、页面js数据缓存 将js数据比如ajax请求来的数据缓存起来是很早就开始做的事情,原来是用一个全局变量__data来存储,现在基于jquery写的两种实现方式。cache这种方式是很普遍的一种实现方式,icache是将数据缓存到dom中,在使用的过程中依赖json.js可以缓存多种类型的数据。cache和icache有一个差别是icache需要检验key和value,这是因为将数据写入dom时需要将不同类型的数据先转换为字符串,这也是可能需要json.js的原因。/** *cache. *paged
2、atacacheincache. */(function($){$.cache={};$.extend($.cache,{map:{},push:function(key,value){$.cache.map[key]=value;},remove:function(key){delete($.cache.map[key]);},clear:function(){$.cache.map={};},get:function(key){return$.cache.map[key];}});})(jQuer
3、y);/** *icache. *pagedatacacheindom. */(function($){$.icache={};$.extend($.icache,{validStr:function(str){returntypeof(str)=='string'?true:false;},data:{containerId:'icacheContainer'},enable:function(){if($('#'+$.icache.data.containerId).length!=0)retu
4、rn;varcontainer=$('
5、
6、!$.icache.validS
7、tr(value))return;varcontainer=$.icache.getContainer();vare=container.find('#'+key);if(e.length==0)e=$('
8、{$.icache.getContainer().find('#'+key).remove();},clear:function(){$.icache.getContainer().empty();}});})(jQuery);
此文档下载收益归作者所有