1

I built the application and it is taking time to load the initial set of data from mongodb and I want to show the loadin gif till the data loading is done. Could you please help me in doing this?

Ramu
  • 97
  • 10
  • *Exact* duplicate of [Displaying loader while meteor collection loads](http://stackoverflow.com/questions/12879762/displaying-loader-while-meteor-collection-loads) – Dan Dascalescu Feb 18 '14 at 04:01

2 Answers2

8

Use Session inside the onReady() callback of the Meteor.subscribe() function, which is invoked when the subscription is completed.

Meteor.subscribe('subscribe_to_this', function onReady(){
         // set a session key to true to indicate that the subscription is completed.
         Session.set('subscription_completed', true);
});

Then make this session value to be returned from your template helper as:

Template.myTemplate.isSubscriptionComplete = function(){
     return Session.get('subscription_completed'); 
}

Now in your html, it is easy to display a loader if data is not loaded or to render a template, if the data has completed loading.

<template name="myTemplate">
    {{#if isSubscriptionComplete }}
          <!-- Data loading is done, so render your template here -->
          {{> yourFinalTemplate}}
    {{else}}
          <!-- Data loading still remaining, so display loader here -->
         <img src="images/load.gif">
    {{/if}}
</template>
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
sohel khalifa
  • 5,602
  • 3
  • 34
  • 46
  • Instead of creating template helpers can we do it by adding css class? i.e `if Session.get("subscription_completed") is true $(".main-container").addClass("loading") else $(".main-container").addClass("loading")` – Rajanand02 Oct 16 '14 at 06:32
2

This could be done with Session variables. This is just an example to get you started:

In your client code:

var yourData;

Meteor.startup(function() {
  Session.set("dataLoaded", false);
});  

...

// Load your initial data
yourData = SampleCollection.find(query).fetch();
Session.set("dataLoaded", true);

A sample template:

<template name="main">
  {{#if dataLoaded}}
     <h1>Welcome to my application!</h1>
     Your data:
     {{#each yourData}}
       ...
     {{/each}}    
  {{else}
     <div>Loading data...</div>
  {{/if}}
</template>

The template helpers:

Template.main.dataLoaded = function() {
  return Session.get("dataLoaded")
}

Template.main.data = function() {
  return yourData;
}
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50