Skip to content Skip to sidebar Skip to footer

Difference Between Find() And Filter(function(){return True;}) In Emberjs

I'm working on a simple restaurant menu. And I need to filter list of dishes accordingly to the category in which we are now in current moment. The problem is in a strange behaviou

Solution 1:

store.filter doesn't query the server, it just filter the already loaded data in the store. In your case because you don't load data in any moment, the filter will return a empty result. You can fix it calling this.store.find('dish'); to load data from the server, so any filter('dish', ...) will be updated.

App.DishRoute = Ember.Route.extend({
  model: function (param) {
    console.log(param.dish_id);

    // pre load the datathis.store.find('dish');
    // filter the prefetch data, when update the filter when new data are loadedreturnthis.store.filter('dish', function(){returntrue;});    
  }
});

This is the updated jsbin http://jsbin.com/akeJOTah/1/edit

This is an overview of the most used store methods:

  1. store.find('dish') => send a request with /dishes
  2. store.find('dish', 1) => send a request with /dishes/1
  3. store.find('dish', { name: 'some' }) => send a request with /dishes?name=some
  4. store.filter('dish', function() { ... }) => client side filtering, don't send a request, just filter the data present in the store
  5. store.filter('dish', function() { ... }, { foo: 'bar' }) => run the find with query (item 3) and perform a client side filtering
  6. store.all('dish') => don't send request, just get all the data loaded in the store

Post a Comment for "Difference Between Find() And Filter(function(){return True;}) In Emberjs"