简体   繁体   English

流星渲染从javascript加载模板

[英]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. 如何从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. 但是在我的情况下#unless#if不是必需的。 Have to load it from script. 必须从脚本加载它。

Very simple. 很简单。 Check out Spacebars built off of Handlebars . 退房Spacebars建关的把手

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

You can render a Template from JS with the Blaze.render() or Blaze.renderWithData() function. 您可以使用Blaze.render()Blaze.renderWithData()函数从JS渲染模板。

The official meteor documentation describes how to use it 官方的流星文档描述了如何使用它
http://docs.meteor.com/#/full/blaze_render 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);

You can load a template from javascript by suing Blaze.render . 您可以通过使用Blaze.render从javascript加载模板。 In your html, create a div and assign it a id, say "rendertemplatehere". 在您的html中,创建一个div并为其分配一个ID,例如“ rendertemplatehere”。

HTML: HTML:

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

In javascript, after writing logic, whenever you want to render template, after your subscription is ready, execute, 在javascript中,编写逻辑后,只要您要呈现模板,就可以在订阅就绪后执行,

Javascript:(Help from webdeb's answer to same question) Javascript :(来自webdeb的同一问题答案的帮助)

    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: Javascript:

Blaze.remove(view);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM