lukaszkups
lukaszkups

Reputation: 5980

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

I have a template with yield section:

  {{>yield}}

In the yield I'm displaying form with fields filled with currently editing category data:

this.route('editCategory', {
    path: '/panel/category/:_id/edit',
    layoutTemplate: 'panelTemplate',
    template: 'editCategoryTemplate',
    data: function(){
        return Categories.findOne(this.params._id);
    },
});

There is a selectbox, (where I'm choosing the parent category) with couple of options. I'm selecting previously chosen option with script:

Template.editCategoryTemplate.rendered = function(){
    $('#categoryParent option[value="'+ this.data.parent +'"]').prop('selected', true);
};

And everything works fine, but after reload the page there is an error:

Exception from Deps afterFlush function: this.data is null

Any help will be appreciated.

Upvotes: 0

Views: 1076

Answers (1)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

It is good idea to put guards:

Instead using this.data.parent write :

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

Upvotes: 1

Related Questions