pixelworlds
pixelworlds

Reputation: 828

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

Upvotes: 2

Views: 3420

Answers (1)

Darshan Sawardekar
Darshan Sawardekar

Reputation: 5075

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.

Upvotes: 4

Related Questions