简体   繁体   中英

Ember.js - how to filter a model?

I'm trying to figure out how to best approach creating a method to pull specific objects based on an attribute using Ember.js.

Right now my model looks like this:

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

If you look at the loop inside the done callback you'll see that it is creating the model using user.id - there is also a user.specialization field. It is that field that I want to be able to filter by.

Any ideas/help would be greatly appreciated!

Thanks!

Rich

You can use filterProperty on any Ember Enumerable like Array or ArrayProxy . It matches for presence by default. You can also pass in an argument to match against each property in the array. You can pair that with a computed property to bind against in your view.

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

See this jsbin for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM