添加加载指示每当应用程序停滞
Add loading indicator whenever app stalls
我有一个动作,导致应用程序挂起几秒钟。我想添加一些加载指示器来显示它在做一些事情,而不是冻结。
我已经尝试了一些东西,其中一些是完整的实验,我认为不会起作用。
首先,这是我在尝试添加任何类型的指示符之前的内容:
filterObserver: function(){
// we needed the debounce because it would crash the app if it took too long
// it has to filter through ~10,000 records
Ember.run.debounce(this, this.filterFoo, 1500);
}.observes('filterValue'),
我认为这将工作,但它似乎等待,直到一切在观察者完成之前,它渲染页面:
controller.js
isLoading: false,
filterObserver: function(){
this.set('isLoading', true);
Ember.run.debounce(this, this.filterFoo, 1500);
this.set('isLoading', false);
}.observes('filterValue'),
template.hbs
<ul class="project-list {{if isLoading 'loading'}}">
{{#each group in foo}}
{{group-item group=group}}
{{/each}}
</ul>
所以,我想也许我需要强制它进行渲染以显示更改。我将整个列表移动到组件中,以便访问组件的rerender
方法:
component.js
export default Ember.Component.extend({
loadingObserver: function() {
this.rerender();
Ember.run.schedule('afterRender', this, function() {
this.sendAction('filterAll');
});
}.observes('isLoading')
});
controller.js
actions: {
filterAll: function() {
Ember.run.debounce(this, this.filterActivities, 1500);
this.set('isLoading', false);
}
}
所以,我想也许Ember的运行循环会起作用。这时,我非常沮丧,但我想尝试所有可能有效的方法:
component.js
export default Ember.Component.extend({
loadingObserver: function() {
this.rerender();
Ember.run.schedule('afterRender', this, function() {
this.sendAction('filterAll');
});
}.observes('isLoading')
});
这些都不起作用。
我知道afterModel
, destroy
等不同的路由方法。此时我的页面已经加载完毕,所以在本例中这些都不起作用。
我相信这是因为Ember会等到观察者中的一切都完成后才渲染模板。因此,我需要一些方法,以便它在模板中任何更改时都显示此指示器,或者等待它完成设置变量并添加类,然后再继续执行操作的代码。
想法吗?想法吗?
为了记录,我知道1.13引入了微光,这将有助于应用程序挂起。然而,我们依赖于几个仍然使用1.11的插件,所以我担心我们暂时无法摆脱它。
编辑
显然run.later
只适用于我的情况,因为我也使用run.debounce
。下面的代码在我的特殊情况下不起作用,因为run.next
认为run.debounce
代码已经完成,而实际上并没有。这应该在大多数情况下工作:
Ember.run.once(this, function() {
this.set('isLoading', true);
});
Ember.run.next(this, function() {
// your code here
// `isLoading` will be true while this is running,
// so your loading indicator will still be present
this.set('isLoading', false);
});
如果你需要一个在Ember的运行循环之外运行的指示器,我的原始答案仍然有效。但是,您可能在绝大多数情况下都能够使用上面的代码。
原始回答
我实际上能够解决这个问题。我只是使用Ember.run.later
,以便让Ember等到所有其他异步事件结束。在我的应用程序中,这相当于:
var _this = this;
this.set('isLoading', true);
Ember.run.debounce(this, this.filterFoo, 1500);
Ember.run.later(function() {
_this.set('isLoading', false);
}, 1500); // I set the timeout to 1500 ms because that's the same as the debounce
我在烬的指南中发现了这个:异步动作的旋转按钮。你可以在Ember的文档中阅读更多关于run.later
的信息。
如果我不这样做,它会尝试一次执行所有的代码。这基本上只是让它等待,直到它完成或超时。
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- Emberjs应用程序加载在除Index之外的所有路由上
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- angular 1.5应用程序中的导航栏
- 在Web应用程序中使用Highcharts javascript
- angularjs+rails应用程序中未显示模板
- 使用acess代币登录Facebook,并通过网络应用程序离线发布
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 科尔多瓦页面类应用程序中的多个谷歌地图
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- Angularjs-utils高亮过滤器在搜索时破坏应用程序
- 在phonegap应用程序内部重定向不起作用
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- Node.js正在更改应用程序以使用集群模块
- 是什么原因导致我的网络应用程序在某些Android设备上停滞?我该如何检测
- 添加加载指示每当应用程序停滞
- AngularJS应用程序有很多文件,由于TCP请求过多而停滞