2

我试图弄清楚如何最好地创建一种方法来使用 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

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

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

谢谢!

富有的

4

1 回答 1

4

您可以filterProperty在任何 Ember 上使用,Enumerable例如ArrayArrayProxy. 它默认匹配存在。您还可以传入一个参数以匹配数组中的每个属性。您可以将其与计算属性配对以在您的视图中绑定。

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

有关示例,请参见此 jsbin

于 2013-06-29T04:27:59.930 回答