onCreated中的流星访问数据上下文

Chr*_*ris 7 meteor

我有一个任务列表,我想在单击其中一个任务时加载相应注释的列表.铁路由器代码:

Router.route('/taskComments/:_id', function () {
        var item = Tasks.findOne(this.params._id);
        this.render('commentList', {data: item});
    },
    {
        name: 'taskComments',
        fastRender: true
    }
);
Run Code Online (Sandbox Code Playgroud)

模板助手:

Template.commentList.helpers({
    comments: function(){
        return Comments.find({taskID: this._id});
    });
Run Code Online (Sandbox Code Playgroud)

我可以在上面的代码片段中访问任务ID(this._id),但它似乎不适用于onCreated:

Template.commentList.onCreated(function(){
    this.subscribe("comments",this._id);
});
Run Code Online (Sandbox Code Playgroud)

当我控制日志时,它给了我以下对象:

在此输入图像描述

请注意,没有_id,数据也为空.

asi*_*ngh 7

您可以Template.currentData()在此回调内部使用以访问模板实例的反应数据上下文.模板销毁时,计算自动停止.

Template.commentList.onCreated(function(){
  var self = this;
  var dataContext = Template.currentData()
  self.subscribe("comments",dataContext._id);
});
Run Code Online (Sandbox Code Playgroud)