冒问一个愚蠢的问题,我会尝试一下.
在RequireJs加载依赖项时是否可以显示进度指示器?
例如:
require(['jquery'], function($) {
// Well, jQuery loaded in quite some time due to a low-speed connection
//
// Or maybe I wanted to show an overlay to prevent user of clicking on UI widgets until the load is complete
});
Run Code Online (Sandbox Code Playgroud)
我不想开始修改RequireJS源,如果有一些插件没有出现在我的谷歌搜索中.
感谢你的帮助.
Ala*_*taR 13
可以显示微调器或进度指示器,但不能显示条本身.我可以获取requirejs的状态(当前正在加载或空闲),但不能加载/需要加载模块的%,因为它们的依赖关系在每个模块加载时进行解析,但不在开始时进行解析.
但是,无论如何,当用户等待时,至少有一个简单的微调器的页面要比空白页面好得多.
无需更改requirejs!所以..
假设我们有文件require.conf.js
require.config({...})
require(["app"], function () {
// main entry point to your hugde app's code
});
Run Code Online (Sandbox Code Playgroud)
并使用html加载它
<script data-main="require.conf" type="text/javascript" src="bower_components/requirejs/require.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是标准的requirejs场景.让我们将指标添加到
<div id="requirejs-loading-panel">
<div id="requirejs-loading-status"></div>
<div id="requirejs-loading-module-name"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
好吧,让我们赶上requirejs的函数require.onResourceLoad并完成所需的所有魔法.它将在每个模块加载时由requirejs调用,通过depjs和所有其他人员传递requirejs的上下文.我们将使用上下文来查明requirejs是否正在加载某些内容.我在计划的计时器调用中完成了它,因为onResourceLoad()仅在加载时调用,而不是在加载完成时调用.需要将此代码添加到require.js.conf中:
require.onResourceLoad = function (context, map, depMaps) {
var loadingStatusEl = document.getElementById('requirejs-loading-status'),
loadingModuleNameEl = document.getElementById('requirejs-loading-module-name');
var panel = document.getElementById('requirejs-loading-panel');
if (loadingStatusEl && loadingModuleNameEl) {
if (!context) { // we well call onResourceLoad(false) by ourselves when requirejs is not loading anything => hide the indicator and exit
panel.style.display = "none";
return;
}
panel.style.display = ""; // show indicator when any module is loaded and shedule requirejs status (loading/idle) check
clearTimeout(panel.ttimer);
panel.ttimer = setTimeout(function () {
var context = require.s.contexts._;
var inited = true;
for (name in context.registry) {
var m = context.registry[name];
if (m.inited !== true) {
inited = false;
break;
}
} // here the "inited" variable will be true, if requirejs is "idle", false if "loading"
if (inited) {
require.onResourceLoad(false); // will fire if module loads in 400ms. TODO: reset this timer for slow module loading
}
}, 400)
if (map && map.name) { // we will add one dot ('.') and a currently loaded module name to the indicator
loadingStatusEl.innerHTML = loadingStatusEl.innerHTML += '.'; //add one more dot character
loadingModuleNameEl.innerHTML = map.name + (map.url ? ' at ' + map.url : '');
}
} else {
}
};
Run Code Online (Sandbox Code Playgroud)
一个问题是:我们无法以某种方式计算出需要加载多少模块,因此我们无法计算加载进度的实际百分比.但是,至少,我们可以发现,无论我们是否正在加载某些内容(以及事件获取当前正在加载模块名称)并将其显示给紧张的用户
kel*_*ran 10
从2.1.19版开始,RequireJS具有onNodeCreatedconfig属性.如果为其分配函数,则每次将<script>元素附加到文档以加载模块时都将调用该函数.
该功能将被提供node,config,moduleName和url参数.通过将事件侦听器附加到node,可以检测脚本何时加载或无法加载.
现在,您可以检测加载过程何时开始和停止,并可以使用该信息通知用户:
require.config({
/* ... paths, shims, etc. */
onNodeCreated: function(node, config, moduleName, url) {
console.log('module ' + moduleName + ' is about to be loaded');
node.addEventListener('load', function() {
console.log('module ' + moduleName + ' has been loaded');
});
node.addEventListener('error', function() {
console.log('module ' + moduleName + ' could not be loaded');
});
},
});
Run Code Online (Sandbox Code Playgroud)
对于IE <9,您需要附加代码来附加事件侦听器.
注意:这仅适用于RequireJS直接请求的模块.插件(requirejs/text等)每个都使用自己的加载机制,不会触发onNodeCreated.有些触发器onXhr,onXhrComplete但是你也可以处理它们:
require.config({
/* ... paths, shims, etc. */
config: {
text: {
onXhr: function(xhr, url) {
console.log('file ' + url + ' is about to be loaded');
},
onXhrComplete: function(xhr, url) {
console.log('file ' + url + ' has been loaded');
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
不,这是不可能的.RequrieJs通过在DOM中创建新的脚本标记来加载模块并侦听load事件.开始和结束加载之间没有更新事件.所以它不是requireJs的限制,而是DOM.
| 归档时间: |
|
| 查看次数: |
7513 次 |
| 最近记录: |