且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EmberJS中的find()和filter(function(){return true;})之间的区别

更新时间:2022-06-17 07:14:48

store.filter 不查询服务器,它只是过滤存储中已经加载的数据。对于您而言,由于您一刻都不加载数据,因此过滤器将返回空结果。您可以调用 this.store.find('dish'); 进行修复,以从服务器加载数据,因此,任何 filter('dish', ...)将被更新。

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 data
    this.store.find('dish');
    // filter the prefetch data, when update the filter when new data are loaded
    return this.store.filter('dish', function(){return true;});    
  }
});

这是更新的jsbin http://jsbin.com/akeJOTah/1/edit

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')=>发送带有/ dishes的请求

  2. store.find('dish',1)=>发送带有/ dishes / 1
  3. 的请求
  4. store.find('dish',{名称:'some'})=>发送带有/ dishes?name = some
  5. 的请求
  6. store.filter('dish',function(){...})=>客户端过滤,不发送请求,只需过滤存储中存在的数据

  7. store.filter('dish',function(){...},{foo:'bar'})=>运行查找查询(项目3)并执行客户端过滤

  8. store.all('dish')=>不发送请求,仅获取存储在商店中的所有数据

  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