简体   繁体   English

动态流星渲染模板

[英]Meteor Rendering Template Dynamically

I have a webapp that consists of several pages and a main page. 我有一个由多个页面和一个主页组成的webapp。 When developing this webapp in Meteor, I segregated the pages into templates. 在Meteor中开发此webapp时,我将页面分为模板。 The webapp shall display different pages for a period of time, meaning that Page A shall be for 60 seconds, then proceed with Page B for maybe 75 seconds and so on. Web应用程序将在一段时间内显示不同的页面,这意味着页面A的显示时间为60秒,然后继续页面B的显示时间为75秒,依此类推。

In meteor I construct a main page that consists of header and footer and a changeable templateholder. 在流星中,我构造了一个包含页眉和页脚以及可更改模板持有人的主页。 So basically the main page looks like this: 所以基本上主页看起来像这样:

<template name='main'>
   {{>header}}
   {{>templateHolder}}
   {{>footer}}
</template>

and the Pages are translated into templates, ie templateA etc. These templates shall replace the templateHolder based on an update on Session object, which also based on some timing (in seconds) that will be executed by using setTimeout JS function. 然后将Pages转换为模板,即templateA等。这些模板应基于Session对象的更新来替代templateHolder ,该更新还基于将使用setTimeout JS函数执行的某些计时(以秒为单位)。

Everything works, however I noticed that the timing of each Page goes haywire. 一切正常,但是我注意到每个Page的时机都一团糟。 When I tested the template individually, they work fine. 当我分别测试template ,它们可以正常工作。 I suspect that the asynch call of setTimeout somehow conflicting each other. 我怀疑setTimeout的异步调用以某种方式相互冲突。

Here is the JS code that changes the template periodically. 这是定期更改模板的JS代码。

Template.main.templateHolder = function(){
  var appIndex = Session.get('currentAppIndex');
  switch(appIndex){
     case 'A':
        return Template['templateA'];
  } //....and so on... with other templates
}

Template.main.created = function() {
  //Query each pages display time and load it into sessions
  // etc...........

  Session.set('currentAppIndex',0); //Initialize the first page
  setTimeout(nextPage,0);
}

function nextPage() {
  //Bunch of line of codes that retrieve time param from settings and calculate
  //Also some simple alogrithm to get next templates.. etc
  //Update the session index object so that it reactively updates the templateHolder

  setTimeout(nextPage,currPageDisplayTime); //currPageDisplayTime is the pages time of display in milliseconds.
}

I am not sure if my way is correct but it managed to display the templates and change them. 我不确定我的方法是否正确,但是它设法显示了模板并进行了更改。 The only concerns is the timing does not work properly. 唯一需要担心的是计时不正确。 What is the best way to change template dynamically? 动态更改模板的最佳方法是什么? Is this prone to any bug in the future? 将来是否容易出现任何错误?

I have found the reason of this buggy behavior. 我发现了这种错误行为的原因。 How do I troubleshoot them? 如何解决它们?

  1. I put JS alert whenever timeout occurs in templates. 每当模板中发生超时时,我都会发出JS alert
  2. Also, alert whenever a session object is updated. 另外,每当会话对象更新时发出alert

The flow of the templates rendering should be like this template A (60 secs) --> template B (75secs)--> template A (which is a looping). 模板渲染的流程应类似于此template A (60 secs) --> template B (75secs)--> template A (循环)。 I noticed that during displaying template B , the alert that I put in template A fires! 我注意到在显示template B ,触发了我放入template Aalert That means that eventhough only one template is being shown at a time, the created template continues to live healthy behind the scene. 这意味着,尽管一次只显示一个模板,但是创建的模板仍在后台健康地生活。 It still continues the process whatever it is designated to do. 无论指定执行什么操作,它都将继续执行该过程。

This made me realize that if both of the templates exist at the same time, I cannot use the same session objects as they will both updating the same things. 这使我意识到,如果两个模板都同时存在,那么我将无法使用相同的会话对象,因为它们都将更新相同的内容。 Turned out that I standardized the use of the session objects across the templates; 原来,我对跨模板使用会话对象进行了标准化。 for example i use Session.set('itemIndex',index) to iterate through the collection in each template, but since the templates are accidentally using the same object, they both update the data at the same time. 例如,我使用Session.set('itemIndex',index)遍历每个模板中的集合,但是由于这些模板意外地使用了同一对象,因此它们都同时更新了数据。

So the JS setTimeout works as intended. 因此,JS setTimeout可以按预期工作。 The problem is the shared session objects. 问题是共享的会话对象。

TL;DR templates don't get destroyed when it is not displayed/changed. TL; DR模板在不显示/更改时不会被破坏。 This can be avoided by destroying it manually or using different objects. 可以通过手动销毁它或使用其他对象来避免这种情况。

P/S: Still looking on how to destroy template after created. P / S:仍在研究创建后如何销毁模板。

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

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