每个流星过滤器 [英] Meteor filter for each

查看:30
本文介绍了每个流星过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对过滤器和每个都有问题.我想创建一个过滤器,它将根据该过滤器更改数据.

i have a problem with filter and each. i want create a filter, that will change data according to that filter.

<select id="filter">
<option value="all">all</option>
<option value="one">one</option>
<option value="two">two</option>
</select>

{{#each datas}}
<span class="badge">{{Name}}</span>
{{/each}}

Template.mytemp.created = function(){
Session.set("activefilter", "all");
};

Template.mytemp.datas = function(){
  var ac = Session.get("activefilter");
  var result = new Array();
  if(ac != undefined){
    var data = RawData.find({filter:ac}).fetch();

      for(var ii = 0; ii < data.length;ii++){
        var nData = NextData.findOne({_id : data[ii].Next_ID});
        result[ii] = {
          Name : nData.Name
        };
      }
  return result;
  }
};

我创建了一个这样的事件处理程序:

i create an event handler like this :

Template.mytemp.events({
  'change #filter':function(){
    Session.set("activefilter",$('#filter').val());
  }
});

每次我更换过滤器时,什么都没有发生,每个过滤器的数据都没有改变.请帮助我如何在过滤器更改时更新数据?

everytime i change the filter, nothing happend, datas on each not changed. please help me how to update datas when filter change?

推荐答案

当您手动迭代游标(使用 each 等)时,您将不得不倒回游标以重置它.取自文档 http://docs.meteor.com/#rewind

When you manually iterate over a cursor (with each etc) you will have to rewind your cursor to reset it. Taken from the docs http://docs.meteor.com/#rewind

forEachmapfetch 方法只能在游标上调用一次.要多次访问游标中的数据,请使用 rewind 重置游标.

The forEach, map, or fetch methods can only be called once on a cursor. To access the data in a cursor more than once, use rewind to reset the cursor.

因此,您可以执行以下操作:

Therefore you can do something like this:

Template.mytemp.datas = function(){
  var ac = Session.get("activefilter");
  var result = new Array();
  if(ac != undefined){
    var cursor = RawData.find({filter:ac});
    var data = cursor.fetch();

      for(var ii = 0; ii < data.length;ii++){
        var nData = NextData.findOne({_id : data[ii].Next_ID});
        result[ii] = {
          Name : nData.Name
        };
      }

    cursor.rewind(); //we rewind our cursor here so that it can be iterated again from the beginning when needed

  return result;
  }
};

这篇关于每个流星过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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