0

I use the template data context inside a template's created function.

Template.temp.created = function() { console.log('this.data'); };

When I go to the page normally--i.e. click the link to the page--I see the console log the correct data object. When I hit the refresh button on the page, this.data is null.

Why?


Also, I am using iron-router to set the data context:

...
this.route('temp', {
    ...
    data: function() { return MyCollection.findOne(someId); },
    ...
}
...
Brian
  • 3,453
  • 2
  • 27
  • 39

1 Answers1

2

If you want to wait until data come then use waitOn.

this.route('temp', {
    waitOn:function(){
      return this.subscribe("nameOfPublishFunction");
    },
    data: function() { return MyCollection.findOne(someId); },
    ...
}

Remember to activate loading hook (thanks @Peppe L-G):

Router.onBeforeAction("loading");

IronRouter docs #waitOn

Update

Here you can find sample meteor app with iron:router package which shows how turning loading hook on and off ( Router.onBeforeAction("loading")) changes availability of data to created and rendered methods.

Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • 2
    Have they changed so the loading hook is included by default? Otherwise you need to add it too in order for it to work. – Peppe L-G Sep 12 '14 at 07:54
  • Hang in there @B-Brock, they're considering changing it to be included by default :) https://github.com/EventedMind/iron-router/issues/819 – Peppe L-G Sep 12 '14 at 13:02