Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

Meteor render loading template from javascript

My question is very simple but I didn't find the exact answer.

<template name="loading">
//loading spineer
</template>

Now I need to hide and show this template with subscription ready. How do I render loading template from javascript. I have tried

{{#unless Template.subscriptionsReady}}
 {{> loading}}
{{/unless}}

and

{{#If Template.subscriptionsReady}}
 {{> loading}}
{{else}}
 content
{{/if}}

But in my case #unless and #if is not required. Have to load it from script.

Upvotes: 0

Views: 619

Answers (3)

Piyush Mahensaria
Piyush Mahensaria

Reputation: 303

You can load a template from javascript by suing Blaze.render. In your html, create a div and assign it a id, say "rendertemplatehere".

HTML:

<div id="rendertemplatehere"></div>

In javascript, after writing logic, whenever you want to render template, after your subscription is ready, execute,

Javascript:(Help from webdeb's answer to same question)

    var view = Blaze.render(Template.loading, document.getElementById('rendertemplatehere'));

And, if you want to remove the template (if its already rendered), when your subscription expires/is not ready, execute,

Javascript:

Blaze.remove(view);

Upvotes: 0

webdeb
webdeb

Reputation: 13211

You can render a Template from JS with the Blaze.render() or Blaze.renderWithData() function.

The official meteor documentation describes how to use it
http://docs.meteor.com/#/full/blaze_render

Example:

// This will render your template to the body and remove it after 3000ms

var view = Blaze.render(Template.loading, document.getElementsByTagName('body')[0]);
setTimeout(function() { Blaze.remove(view) }, 3000);

Upvotes: 1

Nate
Nate

Reputation: 1875

Very simple. Check out Spacebars built off of Handlebars.

{{#if Template.subscriptionsReady}}
  {{> notLoading}}
{{else}}
 {{> loading}}
{{/if}}

Upvotes: 0

Related Questions