我有一系列脚本对象:
_externalLibraries = [{name: 'knockout', url: '...'}, { name: 'knockoutValidation', url: '....'}];
然后我试着写下以下内容:
loadLibraries: function() {
if (_externalLibraries.length === 0) {
return;
}
_externalLibraries.forEach(function(lib){
// Check if the libraries has been registered as "loaded."
var librarysAlreadyLoaded = _loadedLibraries.filter(function(libAlreadyLoaded){
return libAlreadyLoaded.name === lib.name;
});
// If it hasn't been loaded. Load it. This allows for mul;tiple widgets to be on the page.
// Or this file (for what ever reason) to be called multiple times.
console.log(librarysAlreadyLoaded);
if (librarysAlreadyLoaded.length === 0) {
$.getScript(lib.url, function(){
_loadedLibraries.push({name: lib.name});
});
}
});
},
基本上我要做的就是在这个数组中加载库,如果尚未加载的话。如果没有,则将其加载并将名称添加到“已加载库”数组中。
问题,至少在淘汰赛和淘汰赛验证中,“ko未定义”。然后,在页面加载之后,我可以在控制台中输入ko
并看到它实际上已加载。
我可以更改或添加到代码中以使其说“确定我需要等到此阵列中的每个脚本都加载”。然后做我想做的其他事情。