2014-09-02 115 views
0

我有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 

任何帮助将不胜感激。

+0

也许这个页面可以提供一些帮助http://www.meteorpedia.com/read/TypeError_-_Cannot_read_property_nodeName_of_null – 2014-09-02 20:40:32

+0

我的猜测是渲染器上的回调函数在模板实例呈现时调用,但可能数据还没有被检索,因此没有可用的“父”。 – 2014-09-02 20:52:35

+0

我有类似的猜测 - 但如何解决这个问题?如何迫使流星等待/检索这些数据? (它已保存在类别文档集合中) – mrmnmly 2014-09-02 20:55:17

回答

1

它是把守卫好主意:

而不是使用this.data.parent写:

Deps.autorun(function(){ 
    var parentData = this.data && this.data.parent; 
    if(parentData){ 
    $('#categoryParent option[value="'+ parentData +'"]').prop('selected', true); 
    } 
})