red*_*ess 3 javascript meteor spacebars
我有这些嵌套的迭代器:
<ul class='detailViewList'>
{{#each detailsCollection}}
<li id={{_id}} class='detailViewEntry {{checkboxStatus}}'>
<input type="checkbox" class='detailCheckbox'>
<b>{{detail}}</b>
<div class="btn btn-xs btn-danger" id="delete-detail">x</div>
<form class='form-inline'>
<div class="form-group">
<input type="text" id='{{_id}}' name='message' class="form-control" placeholder="message">
</div>
</form>
{{#each messagesCollection}}
{{#if isMessageForThisDetail}}
{{message.message}}
{{/if}}
{{/each}}
</li>
{{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过handlebars路径从docs访问来自子模板的父属性:
{{./ name}}或{{this/name}}或{{this.name}}
但是我需要帮助isMessageForThisDetail器将当前迭代的属性messageCollection与当前迭代的父类进行比较detailsCollection.我的助手看起来像这样:
isMessageForThisDetail: function(){
var message = messagesCollection.findOne({detailId: this._id});
if(message != undefined){
return this._id == message._id;
} else {
console.log('there is no message for this detail');
}
}
Run Code Online (Sandbox Code Playgroud)
但是上下文this._id是消息,而不是_id我要比较消息字段的细节.
您应该能够像这样访问父数据:
isMessageForThisDetail: function() {
var detailId = Template.parentData(1)._id;
var message = messagesCollection.findOne({detailId: detailId});
if(message)
return this._id === message._id;
else
console.log('there is no message for this detail');
}
Run Code Online (Sandbox Code Playgroud)
我被困在一个类似的方式,发现Template.parentData()方法目前在事件处理程序中不起作用(请参阅https://github.com/meteor/meteor/issues/5491).用户Lirbank发布了这个简单的解决方法:
将外部上下文中的数据传递到内部上下文中的html元素,在同一模板中:
{{#each companies}}
{{#each employees}}
<a href="" companyId="{{../id}}">Do something</a>
{{/each}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
现在可以从事件处理程序访问公司ID,例如
$(event.currentTarget).attr('companyId')
| 归档时间: |
|
| 查看次数: |
1847 次 |
| 最近记录: |