I get a collection of records and put them in the template, having them displayed {{#each}} , and I want to display the loading icon until the last DOM node is displayed.
My problem is that I did not find a way to request the callback status / fire for the last element that was added as the last DOM node to update / re-draw.
In my HTML file, it looks something like this:
<template name="stuff"> {{#each items}} <div class="coolView">{{cool_stuff}}</div> {{/each}} </template>
And in the client JS file:
// returns all records of Stuff belonging to the currently logged-in user Template.stuff.items = function () { Session.set('loading_stuff', true); return Items.find({owner: Meteor.userId()}, {sort: {created_time: -1}}); }; Template.stuff.rendered = function() { // every time something new is rendered, there will have been loading of the DOM. // wait a short while after the last render to clear any loading indication. Session.set('loading_stuff', true); Meteor.setTimeout(function() {Session.set('loading_stuff', false);}, 300); };
The session of the variable loading_stuff requested in the Handlebars helper, returning the name of the loading class (with the GIF loader icon), if true .
The reason I am making inconvenient Meteor.setTimeout is because Template.rendered is called after every single element that has been displayed in the Template. Therefore, I need to re-confirm that it is still loading, but prepare for a while to either download the next one or finish rendering them, with a slight pause, until it sets loading_stuff to false .
How can I reliably request / callback at the end of all DOM updates (for a specific template or for the whole page) the correct Meteor path?
Thank you very much.
EDIT
The subscribe() onReady() callback solution only works partially:
The templates are apparently called multiple (2-3) times before the start of rendering, but after that the data returned from the server on which the template for rendering is based. This means that it has โcompletedโ the data loading, but the DOM is still displayed. I will play with Meteor.autorun() and see if I can find a reliable hook somewhere to do it right. In the meantime, I think my initial question still remains:
How to find out when the processing of the entire template / page has ended?
COMPLETION:
At the end of the day, based on how the DOM is structured, the developer has a built-in DOM and applies the appropriate callbacks as best as possible to the elements that he / she wants to check the state. This may seem anti-climatic without problems, but for me the only way to find out which the last element of the template rendered is to have an element with a special identifier displayed at the very end of the template that signals the completion of rendering and attach a .livequery callback to it. I hope that Meteor will include more uniform and global support for checking this condition in the future.