Javascript 为什么嵌套的每个模板都不输出任何内容
虽然这是按预期呈现的:Javascript 为什么嵌套的每个模板都不输出任何内容,javascript,meteor,meteor-blaze,spacebars,Javascript,Meteor,Meteor Blaze,Spacebars,虽然这是按预期呈现的: {{#each Items}} // a collection {{title}} {{/each}} {{#each types}} // another ollection {{refId}} {{/each}} 如果我把它放在一起: {{#each Items}} {{title}} {{#each types}} {{refId}} {{/each}} {{/each}} #每种类型都是空的
{{#each Items}} // a collection
{{title}}
{{/each}}
{{#each types}} // another ollection
{{refId}}
{{/each}}
如果我把它放在一起:
{{#each Items}}
{{title}}
{{#each types}}
{{refId}}
{{/each}}
{{/each}}
#每种类型
都是空的
模板帮助程序:
Template.menuItems.helpers({
restMenuItems: function() {
return RestMenuItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
},
restMenuSideItems: function() {
return RestMenuSideItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
}
});
Template.menuItems.onCreated( function(){
this.subscribe('restMenuItems');
this.subscribe('restMenuSideItems');
});
以及一些模板代码:
{{#每个restMenuItems}
{{{#每个restmenusideims}
{{restRefId}
{{/每个}}
{{/每个}}
即使将{{{{each../restMenuSideItems}}
替换为{{{each../restMenuSideItems}
时,也不会显示任何内容
怎么了?因为#each
子句将数据上下文更改为当前项
如果要在每个项中列出类型
,可以使用获取父数据上下文。
如果types
是父上下文的属性:
{{#每个项目}
{{title}}
{{{每个../types}
{{refId}}
{{/每个}}
{{/每个}}
如果types
是模板帮助器,则可以将父上下文作为参数传递给它:
{{#each Items}}
{{title}}
{{#each types ..}}
{{refId}}
{{/each}}
{{/each}}
并使用上下文进行查询
Template.myTemplate.helpers({
types: function(parent) {
// you now have the parent context here.
// use parent._id etc. where you used this._id in the
// "flat" version.
}
});
谢谢,但还是一样。奇怪的是,这应该行得通。您能否提供更多的代码(例如,您的助手和更完整的模板代码)?项目
和类型
从何而来?刚刚做了@MasterAm在每个集合中都有不同的集合,它工作时不需要使用../,我猜是因为它们没有具有相同键的字段。问题可能不是它是相同/不同的集合,而是它是一个使用查询数据上下文的帮助器。将父数据上下文作为参数传递应该可以做到这一点。