Ember.js-如何过滤模型

Ember.js - how to filter a model?

本文关键字:过滤 模型 js- 何过滤 Ember      更新时间:2023-09-26

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

如有任何想法/帮助,我们将不胜感激!

谢谢!

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

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

有关示例,请参阅此jsbin。