Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将空格键的当前值作为参数传递给meteor/blaze中的新辅助对象_Javascript_Meteor_Meteor Blaze_Spacebars - Fatal编程技术网

Javascript 将空格键的当前值作为参数传递给meteor/blaze中的新辅助对象

Javascript 将空格键的当前值作为参数传递给meteor/blaze中的新辅助对象,javascript,meteor,meteor-blaze,spacebars,Javascript,Meteor,Meteor Blaze,Spacebars,我在Mongo集合中查找年份的独特实例,并将它们作为列表项传递给我的模板。在这些子列表下面,我希望子列表包含给定年份中唯一出现的月份,但我不知道如何将年份传递给我的month()助手,以便限制查询 HTML是 <template name="list"> <ul> {{#each year}} <li> <h2>{{t

我在Mongo集合中查找年份的独特实例,并将它们作为列表项传递给我的模板。在这些子列表下面,我希望子列表包含给定年份中唯一出现的月份,但我不知道如何将年份传递给我的month()助手,以便限制查询

HTML是

        <template name="list">
          <ul>
            {{#each year}}
                <li>
                    <h2>{{this}}</h2>
                </li>
                <ul>
                    {{#each month}}
                        <li>
                            <h3>{{this}}</h3>
                        </li>
                    {{/each}}
                </ul>
              {{/each}}
          </ul>
        </template>
我希望能够将my year()helper当前所在的年份作为参数传递给my month()helper,这样我就可以编辑我的findMonths函数,将它的查询限制为仅在该年发生的事件,但我不知道如何执行此操作,也不知道如何从客户端查询MongoDB一个月(我的所有活动日期均为ISODate格式)


我希望我的问题是清楚的。很抱歉,我知道我可能使用了不正确的术语。

如果我理解正确,您有两个问题-将
传递给您的
助手,并使用该参数使您的查询更具体-是这样吗

要将
年份
传递给帮助程序,请按照
{{{#each things}}
约定(已解释)更改您的代码。因此,您的模板代码将变为:

<template name="list">
  <ul>
    {{#each year in getYears}}
      <li>
        <h2>{{year}}</h2>
      </li>
       <ul>
         {{#each month in (getMonths year)}}
           <li>
             <h3>{{month}}</h3>
           </li>
         {{/each}}
       </ul>
     {{/each}}
   </ul>
 </template>
最后,您的
findMonths
功能。可能有一种不同/更简单的方法,但您可以通过比较更早和更晚的日期(即大于上一年的12月31日,小于下一年的1月1日)来获取年份:

let findMonths = function(year){
    var query = {
      start: {
        $gt: new Date(year - 1, 11, 31),
        $lt: new Date(year + 1, 0, 1)
      }
    }
    var distinctMonths = _.uniq(Events.find(query, {
      sort: {start: 1}, fields: {start: true}
    }).fetch().map(function(x) {
      var d = new Date(x.start);
        return monthNames[d.getMonth()];    
    }), true);
    return distinctMonths;
  };

(如果您想在午夜前完成,可以添加小时、分钟和毫秒)

这太棒了!这正是我想要的。下一步我要做的是找出如何对单个事件执行类似操作。这将更加困难,因为由于2月份,$gt和$lt查询在每个月都不那么容易实现:(我在想可能是某种字符串操作,在这里我检查是否相等?不确定,但非常感谢你的帮助,鲁比!
Template.list.helpers({
    getYears() {
      foundYears = findYears();
      return foundYears;
    },
    getMonths(year) {
      foundMonthNames = findMonths(year);
      return foundMonthNames;
    }
  });
let findMonths = function(year){
    var query = {
      start: {
        $gt: new Date(year - 1, 11, 31),
        $lt: new Date(year + 1, 0, 1)
      }
    }
    var distinctMonths = _.uniq(Events.find(query, {
      sort: {start: 1}, fields: {start: true}
    }).fetch().map(function(x) {
      var d = new Date(x.start);
        return monthNames[d.getMonth()];    
    }), true);
    return distinctMonths;
  };