Waiting for data when dynamically loading templates in Meteor.js

In my meteor.js application, I dynamically load templates,

  {{#DynamicTemplate template=getTemplate data=getData}}
    Default content when there is no template.
  {{/DynamicTemplate}}

When I load the subpatterns, can I wait to display the subpattern until the subfile data that comes from getData is prepared? I mean, is there something like a waitOn function for an iron router that I can do? Thanks you

+4
source share
2 answers

The approach you can take is to subscribe to the template functions created, then when rendering the template, first check each subscription ready(), and if they are not all correct, then visualize the loading display.

<template name="templateWithSubscription">
  {{#if ready}}
    <!-- actual content -->
  {{else}}
    loading...
  {{/if}}
</template>
Template.templateWithSubscription.created = function () {
  this.subscriptions = [
    Meteor.subscribe(/* ... */),
    Meteor.subscribe(/* ... */),
    /* ... */
  ];
};

Template.templateWithSubscription.destroyed = function () {
  _.each(this.subscriptions, function (sub) {
    sub.stop();
  });
};

Template.templateWithSubscription.helpers({
  ready: function () {
    return _.all(Template.instance().subscriptions, function (sub) {
      return sub.ready();
    });
  }
});

(, ).

+3

{{#if Template.subscriptionsReady}}

Template.instance().subscriptionsReady() js-. , , this .

0

All Articles