2013-06-29 30 views
2

我想弄清楚如何最好的方法创建一个方法来拉特定对象的基础上使用Ember.js属性。Ember.js - 如何过滤模型?

现在我的模型看起来是这样的:

App.Resume = Ember.Object.extend()

App.Resume.reopenClass 
    store: {} 

    findAll: -> 
     arr = Ember.ArrayProxy.create() 

     if xhr 
      xhr.abort() 
      return 

     xhr = $.ajax(
      url: '../json/cv.json' 
      dataType: 'json' 
      timeout: 10000 
      ).done (response) => 
       response.users.forEach (user, i) => 
        cv = @findOne(user.personId) 
        cv.setProperties(user) 
        return 
       values = (values for keys, values of @store) 
       arr.set('content', values) 

     arr 

    findOne: (id) -> 
     cv = @store[id] 
     if not cv 
      cv = App.Resume.create 
       id: id 
      @store[id] = cv 
     cv 

如果你看一下循环的完成回调中,你会看到,它正在使用user.id创建模型 - 还有一个user.specialization字段。这是我希望能够过滤的字段。

任何想法/帮助将不胜感激!

谢谢!

丰富

回答

4

您可以在任何灰烬Enumerable使用filterPropertyArrayArrayProxy。它默认匹配存在。您还可以传入参数以匹配数组中的每个属性。您可以将其与计算属性配对以在您的视图中进行绑定。

filtered: function() { 
    return this.get('products').filterProperty('outOfStock') 
}.property('products') 

查看这个jsbin的例子。