繁体   English   中英

流星:在渲染的回调中访问上下文(模板)数据

[英]Meteor: Access the context (template) data in rendered callback

我有一个带有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);
  }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM