Why can't Template.find locate this LI tag?


#1

I must be missing something really stupidly simple. Given this code:

<ul class="items">
  {{#each listOfItems}}
    <li>Thing {{name}}</li>
  {{/each}}
</ul>
Template.myPage.onRendered({
  console.log(this.find('ul.items > li'));
});

I’m getting a null returned by console.log. Why??

EDIT

Ok, I’ve figured out it’s because the {{#each}} statement is still dumping out data after onRendered gets called. Is there some callback I can use after all data on a page is done printing?


#2

if listOfItems is coming from a collection in the db, then you can wait for the subscription to be ready in a Tracker.autorun and then execute your code


#3

Aha! I see.

I went with this code in the template onRendered:

var template = this;

template.subscribe('listOfItems', {
  onReady: function() {
    console.log(template.$('ul.items > li'));
  }
});

The problem was that I was subscribing to the publication on a more global level, and had no way of waiting for it to be ready.