EmberJS中的find()和filter(function(){return true;})之间的区别 [英] Difference between find() and filter(function(){return true;}) in EmberJS

查看:263
本文介绍了EmberJS中的find()和filter(function(){return true;})之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的餐厅菜单。我需要根据当前当前类别来过滤菜肴清单。问题在于 this.store.filter(...)方法的行为异常。它不返回任何内容...

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 behaviour of a this.store.filter(...) method. It doesn't return anything...

我要像这样使用它:

App.DishRoute = Ember.Route.extend({
  model: function (param) {
    return this.store.filter('dish', function(dish) {
      return dish.get('category_id') == param.category_id;
    });
  }
});

但出于测试目的,我使用的是 this.store.filter( 'dish',function(){return true;}); 在我的示例中,此处为 http ://jsbin.com/AcoHeNA/43/

but for the test purpose I'm using this.store.filter('dish', function() {return true;}); in my example here http://jsbin.com/AcoHeNA/43/.

请查看代码,告诉我我做错了什么,或者告诉我应该怎么做

Please review the code and tell me what am I doing wrong or show me the way I should filter the data.

推荐答案

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

这篇关于EmberJS中的find()和filter(function(){return true;})之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆