【发布时间】: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