Skip to content Skip to sidebar Skip to footer

Store.filter() Not Working As Expected Ember.js (trying To Search Models)

I am trying to implement a search system here and I am having some trouble with store.filter() First of all I can't find any good documentation on the store.filter() method aside f

Solution 1:

You just need to pass in a function that returns true/false for including the record

App.IndexRoute = Ember.Route.extend({
  model: function() {
    returnthis.store.filter('color', function(item){
      return item.get('color')=='red';
    });
  }
});

http://emberjs.jsbin.com/OxIDiVU/647/edit

If you want to make a call back to the server at the same time (find by query) you include the optional query param, the below example will call /colors?color=green

App.IndexRoute = Ember.Route.extend({
  model: function() {
    returnthis.store.filter('color', {color:'green'}, function(item){
      return item.get('color')=='red';
    });
  }
});

/**
    Takes a type and filter function, and returns a live RecordArray that
    remains up to date as new records are loaded into the store or created
    locally.

    The callback function takes a materialized record, and returns true
    if the record should be included in the filter and false if it should
    not.

    The filter function is called once on all records for the type when
    it is created, and then once on each newly loaded or created record.

    If any of a record's properties change, or if it changes state, the
    filter function will be invoked again to determine whether it should
    still be in the array.

    Optionally you can pass a query which will be triggered at first. The
    results returned by the server could then appear in the filter if they
    match the filter function.

    Example

    ```javascript
    store.filter('post', {unread: true}, function(post) {
      return post.get('unread');
    }).then(function(unreadPosts) {
      unreadPosts.get('length'); // 5
      var unreadPost = unreadPosts.objectAt(0);
      unreadPost.set('unread', false);
      unreadPosts.get('length'); // 4
    });
    ```

    @methodfilter@param {String or subclass of DS.Model} type@param {Object} query optional query
    @param {Function} filter@return {DS.PromiseArray}
  */

Post a Comment for "Store.filter() Not Working As Expected Ember.js (trying To Search Models)"