希望知道Meteor中的父/子模板渲染顺序

时间:2016-03-20 07:34:12

标签: templates meteor

我的应用开发确实存在问题,希望能获得专家级的帮助。

<template name="inspection_go">
  <div class="transitions-content">
    <div class="row toolbar">
      <div class="col s1">
        {{#if previous_group}}
          <a href="{{pathFor 'inspection_go' inspection_id=inspection._id group_id=previous_group._id}}" data-transition-out="slideRightOut" data-transition-in="slideRightIn" class="back-button btn-large btn-floating waves-effect waves-light blue" ><i class="mdi-image-navigate-before"></i></a>
        {{else}}
          <a href="{{pathFor 'inspection_view' _id=inspection._id}}" data-transition-out="slideRightOut" data-transition-in="slideRightIn" class="back-button btn-large btn-floating waves-effect waves-light blue" ><i class="mdi-hardware-keyboard-backspace"></i></a>
        {{/if}}
      </div>
      <div class="col s4">
        {{#if next_group}}
          <a href="{{pathFor 'inspection_go' inspection_id=inspection._id group_id=next_group._id}}" data-transition-out="slideRightOut" data-transition-in="slideRightIn" class="back-button btn-large btn-floating waves-effect waves-light blue" ><i class="mdi-image-navigate-next"></i></a>
        {{else}}
          <a href="{{pathFor 'inspection_view' _id=inspection._id}}" data-transition-out="slideRightOut" data-transition-in="slideRightIn" class="toolbar_button btn waves-effect waves-light green" ><i class="mdi-navigation-check left"></i>Complete Inspection</a>
        {{/if}}
      </div>
    </div>
    <div class="content">
      {{> answer_draw_at_a_location group=current_group building=building inspection_id=inspection._id}}
    </div>
  </div>
</template>


<template name="answer_draw_at_a_location">
  <div class="row" style="margin-top: 20px; height: 100%;">
    <div>
      {{#each questions_in_group}}
        <div style="width: 100%;">{{> question_to_answer question_id}}</div>
      {{/each}}
    </div>
  </div>
</template>
<template name="question_to_answer">
  ...
</template>

上面的代码显示了父子模板关系。

这是父/子模板的onRendered。

  Template.answer_draw_at_a_location.onRendered(function() {
    console.log("parent");
  });

  Template.question_to_answer.onRendered(function() {
    console.log("child");
  });

问题如下。

当我首先进入inspection_go页面时,首先调用answer_draw_at_a_location,即父模板的onRendered函数,之后调用question_to_answer,即子模板的onRendered函数。

因为当我点击next_group或previous_group链接时,它识别相同的模板,在iron:router中的inspection_go,它不会调用&#34; answer_draw_at_a_location&#34; onRendered函数,所以我改为使用#each来调用它,如下所示。

{{#each current_groups}}
  {{> answer_draw_at_a_location group=this building=../building inspection_id=../inspection._id}}
{{/each}}

但是现在当我点击prevoius_group或next_group标签时,它首先执行子模板的onRendered函数(question_to_answer),然后执行父模板的onRendered函数(answer_draw_at_a_location)。

所以我希望知道它为什么会发生,至少,有没有办法首先执行父模板onRendered函数,然后执行子模板onRendered,反之亦然。

请帮帮我。

0 个答案:

没有答案