2013-06-29 31 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的例子。