標籤:dojo js
使用dojo後一直糾結js檔案內容有更新時如何載入,這兩天終於把問題解決了,原來想加一個庫檔案代替dojo相應的函數,但發現dojo庫糾緾得太緊了,就在原檔案上改。
載入分兩種情況一種dojo.text!載入或不用執行代碼,另外一個是作為庫載入,這兩個要分開處理。
第一種是直接在載入的url後面加入版本號碼“?v=xxx"。
toUrl = req.toUrl = function(name, referenceModule){var moduleInfo = getModuleInfo(name+"/x", referenceModule),url= moduleInfo.url; url=url.substring(0, url.length-5) if (req.version){ url += "?"+req.version; name+="?"+req.version; }return fixupUrl(moduleInfo.pid===0 ?// if pid===0, then name had a protocol or absolute path; either way, toUrl is the identify function in such casesname :// "/x.js" since getModuleInfo automatically appends ".js" and we appended "/x" to make name look like a module idurl//url.substring(0, url.length-5));
另外一種是在載入庫檔案時添加版本號碼“?v=xxx"。
compactPath = function(path){ var p=path;var result = [],segment, lastSegment;path = path.replace(/\\/g, '/').split('/');while(path.length){segment = path.shift();if(segment==".." && result.length && lastSegment!=".."){result.pop();lastSegment = result[result.length - 1];}else if(segment!="."){result.push(lastSegment= segment);} // else ignore "."} var url=result.join("/"); if(p.indexOf('/x.js')==-1 && req.version && p.indexOf('.js')!=-1){ url+="?"+req.version; //console.log('compactPath result:',path,result); }return url;},
使用時在require庫增加version屬性即可。
require.version=‘v0.1‘;