如何进行计算,过滤的属性? [英] How do I make a computed, filtered property?

查看:88
本文介绍了如何进行计算,过滤的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的东西:

Epic = Ember.Object.extend({
    children:[],
    children_filtered: function(){        
        return this.get("children").filterProperty("archived",false);
    }.property("children"),
    init: function() {
      this._super();
      this.set("children", Ember.ArrayController.create({content:[]}) );
      this.set("stories",  Ember.ArrayController.create({content:[]}) );
    },
});

注意children_filtered计算属性。

Note the children_filtered computed property.

如果我在视图中使用children_filtered ...

If I use children_filtered in a view...

{{#each content.children_filtered }}
  hi
{{/each}}

我的应用程序挂起cpu @ 100%

My application hangs with cpu @ 100%

任何想法我做错了什么?有一个更好的模式可以跟踪一个具有项列表的对象加上过滤的项目列表吗?

Any ideas what I'm doing wrong? Is there a better pattern to follow for an object that has a list of items plus a list of filtered items?

推荐答案

你的问题是,您需要将计算的属性设置为可缓存。否则,在 #each 的每次迭代时重新计算其值。有人讨论过可缓存应该是所有计算属性的默认值。

Your problem is that you need the computed property to be set as cacheable. Otherwise, its value is recomputed upon every iteration of the #each. There has been discussion about whether cacheable should be the default for all computed properties.

children_filtered: function(){        
  return this.get("children").filterProperty("archived",false);
}.property("children").cacheable()

这是一个jsFiddle示例: http://jsfiddle.net/ebryn/9ZKSY/

Here's a jsFiddle example: http://jsfiddle.net/ebryn/9ZKSY/

这篇关于如何进行计算,过滤的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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