加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dojo实现加载新版本js库

发布时间:2020-12-16 21:26:18 所属栏目:百科 来源:网络整理
导读:使用dojo后一直纠结js文件内容有更新时如何加载,这两天终于把问题解决了,原来想加一个库文件代替dojo相应的函数,但发现dojo库纠緾得太紧了,就在原文件上改。 加载分两种情况一种dojo.text!加载或不用执行代码,另外一个是作为库加载,这两个要分开处理。

使用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 cases
				name :
				// "/x.js" since getModuleInfo automatically appends ".js" and we appended "/x" to make name look like a module id
				url//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';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读