var CLoader = this.CLoader = { getCacheKey: function() { APSMX-client-abcdefg APSMX-admin-abcdefg return appcfg["app-id"]+"-"+ appcfg["ep-module"]+"-"+ appcfg["ins-id"]; }, クライエント側にキャッシュされているデータ項目をcached配列に入れる getCacheSub: function( ctype, b_cache, cache_key, cached ) { if ( b_cache ) { var val; switch( ctype ) { case "LL": val = localStorage.getItem(cache_key); break; case "SS": val = sessionStorage.getItem(cache_key); break; } var vx = (val===null)?{}:JSON.parse(val); for( var key in vx ) { cached[key] = vx[key]["cid"]; } } }, getCacheList: function() { var cached = {}; var cache_key = this.getCacheKey(); this.getCacheSub("LL",appcfg["cfc-local"], cache_key,cached); this.getCacheSub("SS",appcfg["cfc-session"], cache_key,cached); return cached; }, サーバ側から返ってきたデータ項目を localStorageとsessionStorageに記録する storeCacheSub: function( ctype, b_cache, cache_key, ldr_items, items_in, items_out) { if ( b_cache ) { var val; switch( ctype ) { case "LL": val = localStorage.getItem(cache_key); break; case "SS": val = sessionStorage.getItem(cache_key); break; } var cached = (val===null)?{}:JSON.parse(val); var b_changed = false; var items_this = {}; for( var ciname in ldr_items ) { データのctype(localStorage:LL,sessionStorage:SS)がマッチしたら。。。 if ( ctype == ldr_items[ciname] ) { サーバ側から返ってきた項目を一つ一つ調べる。 if ( ciname in items_in ) { もし「変更無し(=)」だったら、 if ( items_in[ciname]==="=" ) { クライエント側のキャッシュから、そのデータ項目を取り出して、 結果配列(items_out)に入れる if ( ciname in cached ) { items_out[ciname] = items_this[ciname] = cached[ciname]; } } else { もし「変更無し(=)」ではなかったら、サーバー側からデータが届いているはずなので、 そのデータを結果配列(items_out)に入れる。 items_out[ciname] = items_this[ciname] = items_in[ciname]; b_changed = true; } } } } if ( b_changed ) { var val = JSON.stringify(items_this); switch( ctype ) { case "LL": localStorage.setItem(cache_key,val); break; case "SS": sessionStorage.setItem(cache_key,val); break; } } } else { キャッシュをアップデートしない場合、 単純に、items_inをitems_outにコピーする。 for( var ciname in ldr_items ) { if ( ctype == ldr_items[ciname] ) { if ( ciname in items_in ) { items_out[ciname] = items_in[ciname]; } } } キャッシュをアップデートしない場合、 キャッシュはクリアする switch( ctype ) { case "LL": localStorage.removeItem(cache_key); break; case "SS": sessionStorage.removeItem(cache_key); break; } } }, storeCache: function( items_in ) { var items_out = {}; var cache_key = this.getCacheKey(); this.storeCacheSub("LL",appcfg["cfc-local"], cache_key,appcfg["ldr-items"],items_in,items_out); this.storeCacheSub("SS",appcfg["cfc-session"], cache_key,appcfg["ldr-items"],items_in,items_out); return items_out; }, runMain : function( resp ) { itemsをthis.itemsとして記録しておく。 this.items = this.storeCache(resp.items); メインscriptの実行 CJsp.execute(this.items["main"]["data"]); }, updateLoader: function() { var url_server = appcfg["url-loader"]; var xhr = new XMLHttpRequest(); xhr.open("GET",url_server,true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); キャッシュをクリアしてサーバー側から再読み込み xhr.setRequestHeader("Cache-Control","max-age=0"); xhr.addEventListener("load",function(){ if ( xhr.status == 200 ) { CJsp.insertCode(xhr.responseText); } else { var s = ""; s += "status:" + xhr.status + "\n"; s += "statusText:" + xhr.statusText; console.log("CLoader.updateLoader",s); } }); xhr.send(); }, main: function() { var requ = { cmd:"load", ローダーのcid "ldr-cid":appcfg["ldr-cid"], ローダーが実行された回数 "ldr-index":CWagv.inc("ldr-index",0), クライエント側にあるキャッシュ "cached":this.getCacheList() }; console.log("requ:",requ); CAjax.send(requ,this,function(resp){ console.log("resp:",resp); if ( resp["cmd-update-loader"] === 1 ) { ローダーを再読み込みする this.updateLoader(); } else { mainスクリプトを実行する this.runMain(resp); } }); } };