Ember.js - 如何过滤模型?

时间:2013-06-29 02:59:05

标签: javascript model filter ember.js coffeescript

我正在试图找出如何使用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字段。这是我希望能够过滤的字段。

非常感谢任何想法/帮助!

谢谢!

1 个答案:

答案 0 :(得分:4)

您可以在filterPropertyEnumerable之类的任何Ember Array上使用ArrayProxy。它默认匹配存在。您还可以传入一个参数来匹配数组中的每个属性。您可以将其与计算属性配对,以在视图中进行绑定。

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

有关示例,请参阅此jsbin