How to show waiting when code process for along time (report generate)?


#1

I want to show spinner waiting when I generate report for along time.
Example

// Template
{{#if data}}
   Show result
{{else}}
    No result
{{/if}}

// Helper
Template.myTemplate.helpers({
   data: function(){
      var data = ..............
       // get data from database and calculate
       // waiting
      
      if(data.count() > 0){
          return data;
      }else{
          return false; 
      }
   }
})

#2
...
{{#if waiting}}
  {{> spinner}}
{{/if}}

Template.myTemplate.helpers({
  data: function () {
    //waiting
    Session.set("waiting", true);

   if (data.count() > 0) {
     // not waiting anymore
     Session.set("waiting", false);
  },
  waiting: function () {
    return Session.get("waiting");
  }
})

#3

Thanks, I will try :smiley:


#4

I tried, but don’t show spinner when waiting the result.
And then I custom like this

Template.myTemplate.onRendered(function(){
     //waiting
     Session.set("waiting", true);
});

Template.myTemplate.helpers({
  data: function () {

   if (data.count() > 0) {
     // not waiting anymore
     Session.set("waiting", false);
  },
  waiting: function () {
    return Session.get("waiting");
  }
})

It show spinner forever.


#5

See here or Google “javascript sleep”.


#6

Now I see why Session should not be in documentation :smiley:

And set that waiting variable true on same spot as u are instructing app to start generating report
If you really need to use Session, than use Session.setDefault in onRendered, to not overwrite it for no reason.


#7

Are you setting the session before returning your data?


#8

Yes I setting the session before.