Ember.js:折叠/推迟昂贵的观察者或计算属性 [英] Ember.js: Collapsing/deferring expensive observers or computed properties

查看:220
本文介绍了Ember.js:折叠/推迟昂贵的观察者或计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  topContributor:在一个Ember应用程序中,说你有一个观察者或属性来观看数组, (function(){
// ...循环文章(因此缓慢)...
})。property('articles。@ each.author')

通过ember数据更新文章数组,反复触发属性功能总共 articles.length times。



有没有办法将更新折叠成一个懒惰更新当所有的更改都完成并且runloop被刷新?

解决方案

感谢@wagenet和@krisselden的以下指针: / p>

此刻,当绑定被延迟(延迟)时,观察者和扩展计算属性会立即触发。将来,可能会延迟使用



同时,您可以使用 Ember.run.once 作为调度延迟函数调用的解决方法,只能运行一次。计算的属性,我想,可以很容易地变成观察者遵循相同的模式。这是一个例子:

  updateTopContributor:function(){
// ...循环文章(因此慢)...
this.set('topContributor',...);
},

_updateTopContributorObserver:(function(){
Ember.run.once(this,'updateTopContributor');
})。 @ each.author')


In an Ember app, say you have an observer or a property that watches an array, like so:

topContributor: (function() {
  // ... loop over articles (hence slow) ...
}).property('articles.@each.author')

Updating the articles array, through ember-data for instance, repeatedly triggers the property function for a total of articles.length times.

Is there a way to collapse the updates into one lazy update when all the changes have finished and the runloop is flushed?

解决方案

Thanks to @wagenet and @krisselden for the following pointers:

At the moment, while bindings are deferred (lazy), observers and by extension computed properties trigger immediately. In the future, they might become deferred as well.

In the meantime, you can use Ember.run.once as a workaround to schedule a deferred function call, which will be run only once. Computed properties, I suppose, can be easily turned into observers to follow the same pattern. Here is an example:

updateTopContributor: function() {
  // ... loop over articles (hence slow) ...
  this.set('topContributor', ...);
},

_updateTopContributorObserver: (function() {
  Ember.run.once(this, 'updateTopContributor');
}).observes('articles.@each.author')

这篇关于Ember.js:折叠/推迟昂贵的观察者或计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆