【问题标题】:pass current value of spacebars:this as argument to new helper in meteor/blaze传递空格键的当前值:this 作为流星/火焰中的新助手的参数
【发布时间】:2017-12-03 20:46:30
【问题描述】:

我在 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>

而 JS 是……

  let monthNames = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ];

  let findYears = function(){
    var distinctYears = _.uniq(Events.find({}, {
      sort: {start: 1}, fields: {start: true}
    }).fetch().map(function(x) {
      var d = new Date(x.start);
      return d.getFullYear();
    }), true);
    return distinctYears;
  };

  let findMonths = function(){
    var distinctMonths = _.uniq(Events.find({}, {
      sort: {start: 1}, fields: {start: true}
    }).fetch().map(function(x) {
      var d = new Date(x.start);
        return monthNames[d.getMonth()];    
    }), true);
    return distinctMonths;
  };

  Template.list.onCreated( () => {
    let template = Template.instance();
    template.subscribe( 'events' );
  });

  Template.list.helpers({
    year() {
      foundYears = findYears();
      return foundYears;
    },
    month() {
      foundMonthNames = findMonths();
      return foundMonthNames;
    }
  });

我希望能够将我的 year() 助手当前所在的年份作为我的 month() 助手的参数传递,以便我可以编辑我的 findMonths 函数以将它的查询限制为仅属于该事件的事件年,但我不知道如何做到这一点,也不知道如何从客户端查询 MongoDB 只有一个月(我所有的活动日期都是 ISODate 格式)。

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

【问题讨论】:

    标签: javascript meteor meteor-blaze spacebars


    【解决方案1】:

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

    要将year 传递给帮助程序,请按照{{#each thing in things}} 约定(解释为here)更改您的代码。所以你的模板代码会变成:

    <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>
    

    然后您的 year 助手将保持不变(除了我已更改名称以使其更清晰!),而您的 month 助手将只接受该参数:

    Template.list.helpers({
        getYears() {
          foundYears = findYears();
          return foundYears;
        },
        getMonths(year) {
          foundMonthNames = findMonths(year);
          return foundMonthNames;
        }
      });
    

    最后,你的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 查询都不会那么容易实现:(我在想可能是某种字符串操作,我检查是否相等?不确定,但谢谢非常感谢您的帮助 rubie!
    猜你喜欢
    • 2016-11-17
    • 2014-07-14
    • 2016-05-18
    • 2017-07-08
    • 2023-03-16
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    相关资源
    最近更新 更多