store.filter() not working as expected ember.js (Trying to search models)

跟風遠走 提交于 2019-12-20 04:41:22

问题


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 from here: http://emberjs.com/guides/models/frequently-asked-questions/

So I am using the example provided on that page as a guideline.

This is what I have in my code

App.ApplicationController = Ember.ObjectController.extend({
    isDropdown: false,
    actions: {
        handle_search: function() {
            var search_text = this.get('search_text');
            if (search_text) {
                this.set('isDropdown', true);
                return this.store.filter('procedure', {proc_name: search_text}, function(procedure) {
                    console.log(procedure);
                });
            }
        }
    }
});

but when I log what is being returned, it's returning basically every single model. Instead of no results or a limited amount of results.

On top of that procedure it self is not the model's object, it's some other thing.

So my question is how do I get the actual model with the fields and how can I ensure that the store is actually filtering the results?


回答1:


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

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.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() {
    return this.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
    });
    ```

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


来源:https://stackoverflow.com/questions/24208906/store-filter-not-working-as-expected-ember-js-trying-to-search-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!