繁体   English   中英

流星:单击时在模板内渲染模板

[英]Meteor: render template inside a template on click

我有一个已加载模板的Meteor应用程序。 在该模板内,我有一个按钮。 单击按钮时,我希望该应用程序呈现或切换到恰好是模式的另一个模板。

我不知道如何从点击事件功能中渲染另一个模板。

有任何想法吗? 这是下面的代码。

search.html

<template name='search'>

    <button class='btn btn-primary' id='showModalButton'>Show Modal</button>

</template>

<template name='searchCustomer'>

    <!-- set up the modal to start hidden and fade in and out -->
    <div id="myModal" class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Modal title</h4>
        </div>
          <!-- dialog body -->
          <div class="modal-body">
              My modal body
          <div class="modal-footer"><button type="button" class="btn btn-primary">OK</button></div>
        </div>
      </div>
    </div>

</template>

search.js

Template.search.onRendered(function() {
    'click #showModalButton': {
       // WHAT DO I DO HERE TO RENDER OR CALL THE 
       // SEARCHCUSTOMER TEMPLATE WITH THE MODAL?
    }

});

Template.searchCustomer.onRendered(function() {

    $("#myModal").on("show", function() {    
          $("#myModal a.btn").on("click", function(e) {
              $("#myModal").modal('hide');
          });
      });

      $("#myModal").on("hide", function() {    
          $("#myModal a.btn").off("click");
      });

      $("#myModal").on("hidden", function() {  
          $("#myModal").remove();
      });

      $("#myModal").modal({                    
        "backdrop"  : "static",
        "keyboard"  : true,
        "show"      : true   // show the modal immediately                  
      });

});

我还没有尝试过,但是我相信您可以使用模板助手和Session变量来完成此任务。

<body>
  {{> search }}
  {{#if showModal }}
    {{> searchCustomer }}
  {{/if}}
</body>

然后在您的js文件中...

Template.search.helpers({
    'showModal': function(){
        return Session.get('showModal');
    }
    'click #showModalButton': function(){
        Session.set('showModal', true);
    }
);

仅当'showModal'Session变量为true时,searchCustomer模板才会呈现。

暂无
暂无

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

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