流星破坏子模板

问题描述

我有一个称为子模板的父模板。我想通过按一个按钮杀死子模板。

父模板


    <template name="tracker">
         <span id="destroyChild" class="btn" title="destroyTable"></span>
    
         {{<child}}
    </template>

然后,我有一个事件试图破坏子模板

'click span[id=destroyChild]'(e,template) {
            //This works to remove the Parent template
            //Blaze.remove(template.view);
 
           Blaze.remove('what do I put here?');
        },

我找不到要用作删除子模板的参数的任何东西。我不断收到Uncaught Error: Expected template rendered with Blaze.render

我给子模板一个ID,并尝试用selector调用它,但是没有运气。有任何想法吗?谢谢!

解决方法

您不应强制更改布局。 Blaze是一种反应式引擎,因此应以声明方式决定一切,例如:

<template name="tracker">
   <span id="destroyChild" class="btn" title="destroyTable"></span>

   {{#if show}}
   {{> child}}
   {{/if}}
</template>
Session.setDefault('show',true);
Template.tracker.helpers({
  show() {
    return Session.get('show');
  }
});

...

  'click span[id=destroyChild]'(e,template) {
     Session.set('show',false);
  },