How do I make a computed, filtered property?

Deadly 提交于 2019-12-23 08:56:38

问题


I've got something like this:

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:[]}) );
    },
});

Note the children_filtered computed property.

If I use children_filtered in a view...

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

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?


回答1:


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()

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



来源:https://stackoverflow.com/questions/8871868/how-do-i-make-a-computed-filtered-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!