我有一个带有 yield
部分的模板:
{{>yield}}
在yield
中,我显示表单,其中的字段填充了当前正在编辑的类别数据:
this.route('editCategory', {
path: '/panel/category/:_id/edit',
layoutTemplate: 'panelTemplate',
template: 'editCategoryTemplate',
data: function(){
return Categories.findOne(this.params._id);
},
});
有一个选择框(我在其中选择父类别),其中有几个选项。我正在使用脚本选择之前选择的选项:
Template.editCategoryTemplate.rendered = function(){
$('#categoryParent option[value="'+ this.data.parent +'"]').prop('selected', true);
};
一切正常,但重新加载页面后出现错误:
Exception from Deps afterFlush function: this.data is null
任何帮助将不胜感激。
最佳答案
设置守卫是个好主意:
而是使用 this.data.parent
编写:
Deps.autorun(function(){
var parentData = this.data && this.data.parent;
if(parentData){
$('#categoryParent option[value="'+ parentData +'"]').prop('selected', true);
}
})
关于javascript - Meteor:在渲染的回调中访问上下文(模板)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631767/