2015-02-10 55 views
0

这是代码:即使我使用waitOn,集合也没有及时呈现?

Meteor.publish('singleDocument', function(documentId) { 
    return Documents.find(documentId); 
}); 

this.route("documentPage", { 
    path: "/documents/:_id", 
    waitOn: function() { 
    return Meteor.subscribe("singleDocument", this.params._id); 
    }, 
    data: function() { 
    return Documents.findOne(this.params._id); 
    } 
}); 

Template.documentPage.rendered = function() { 
    Tracker.autorun(function() { 
     console.log(this.content); 
    }); 
}; 

正如你可以看到我已经设置的一切,我等待的收集与waitOn。但console.log(this.content);仍然会返回undefined,好像该集合尚未加载。

可能是什么问题?

回答

1

this自动运行中没有引用用于呈现模板的数据上下文。在渲染功能,自动运行功能之外(你不应该需要使用自动运行的话),你可以使用this.data指用来渲染模板的数据上下文,所以请尝试以下操作:

Template.documentPage.rendered = function() { 
    console.log(this.data); 
}; 

根据下面的评论更新:

你应该能够使用Template.currentData监听数据环境的变化:

Template.documentPage.rendered = function() { 
    Tracker.autorun(function(){ 
     console.log(Template.currentData()) 
    }) 
}; 
+0

你说得对,它的工作。谢谢。那么在Tracker.autorun中没有办法使用它?如果我想在每次收集更新时要运行的函数中使用它, – alexchenco 2015-02-11 03:34:29

+0

@alexchenco,我认为你可以使用Template.currentData。看到我更新的答案。 – 2015-02-11 07:28:50

相关问题