【发布时间】:2018-11-25 01:18:51
【问题描述】:
我希望有一个完全异步的方法来在客户端加载模块的文件,而不需要额外的工具,例如 requireJS。我的模块模板遵循"Revealing module pattern" template。
在我的文件root.js我有
root = (function(thisModule){
//I'm stuck here, I know I need to add the already
//existent properties of thisModule into here
//when child.js is loaded first
function A(){...}
function B(){...} //public method
return{
B:B
};
})(root || {});
在我的文件child.js我有
root = root || {};
root.child = (function(){
function C(){...}
function D(){...} //public methods
return{
D:D
};
})();
如何重写root.js 中的第一个模块使文件的加载顺序无关紧要?也就是说,下面的代码永远不会抛出异常。
$.when($.getScript("root.js"), $.getScript("child.js")).done(function(){
root.B;
root.child.D;
});
【问题讨论】:
-
所以你希望在两个脚本都加载后能够访问
root.B和root.child.D,是吗? -
我认为 Promise 是这里的必经之路。但你说你不想那样做。在看到加载之前,您是否同意轮询的想法?为什么反对承诺?
-
@mccambridge 你的意思是
getScript中的承诺吗?我希望文件的加载完全异步。 -
@CertainPerformance,是的,并且独立于文件加载顺序。有可能吗?
-
@CertainPerformance 我的问题是当
child.js首先加载时,root.child会被root.js的加载删除
标签: jquery performance asynchronous module getscript