Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor access data context in onCreated

Tags:

meteor

I have a list of tasks and I want to load a list of corresponding comments when I click one of the Tasks. Iron router code:

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

Template helpers:

Template.commentList.helpers({
    comments: function(){
        return Comments.find({taskID: this._id});
    });

I am able to access the task id (this._id) in the above snippet, but it does not seem to work for onCreated:

Template.commentList.onCreated(function(){
    this.subscribe("comments",this._id);
});

When I console log this it gives me the following object:

enter image description here

Notice that there is no _id and data is also null.

like image 947
Chris Avatar asked Feb 01 '16 16:02

Chris


1 Answers

You can use Template.currentData() inside of this callback to access reactive data context of the template instance. The Computation is automatically stopped when the template is destroyed.

Template.commentList.onCreated(function(){
  var self = this;
  var dataContext = Template.currentData()
  self.subscribe("comments",dataContext._id);
});
like image 97
asingh Avatar answered Nov 03 '22 16:11

asingh