【问题标题】:Meteor Blaze.renderWithData how to InsertAfter is it possible?Meteor Blaze.renderWithData 如何 InsertAfter 是可能的吗?
【发布时间】:2015-07-06 22:03:22
【问题描述】:

我在节点之后而不是之前插入模板时遇到问题。例如:

//Html looks like this
<div class="questions">
 <div class="question"></div>
 <div class="question"></div>
 <div class="question"></div>
</div>

<template name="question">
  <div class="question"></div>
</div>
<template name="questionExtraInfo">
  <div class="extra"></div>
</template>

我正在尝试获得以下信息:

<div class="questions">
 <div class="question"></div>
 <div class="extra"></div>
 <div class="question"></div>
 <div class="question"></div>
</div>

在问题事件中调用 blaze 渲染

Template.question.events({
  'click .more-details': function () {
     var instance = Template.instance();
     Blaze.renderWithData(Template.questionExtraInfo, {}, document.querySelector('.questions'), instance.find('.question')));
});

我只能弄清楚如何渲染它之前或之后如何?

 <div class="extra"></div>
 <div class="question"></div>

 <div class="question"><div class="extra"></div></div>

【问题讨论】:

  • 如果有人遇到同样的问题,只需添加空 div 以暂时将其保存在需要的地方

标签: meteor meteor-blaze


【解决方案1】:

我认为更好的方法是利用反应性:

将您的 questions 模板更改为:

<template name="question">
  <div class="question"></div>
  {{# if shouldIncludeExtra }}
    {{> questionExtraInfo }}
  {{/if}}
</template>

上面的模板应该在每个循环中。

然后在你的 js 中类似:

Template.question.helpers({
  'shouldIncludeExtra': function() {

     // replace 'n' with the actual index. I think `this.index` is 
     // provided within #each blocks, or you can use the new `@each` helper.
    var index = n;

    return Session.get('shouldIncludeExtra' + index);

  }
});

然后,在您的点击事件中,您将基于索引的会话变量设置为true

Template.questions.events({
  'click .question': function(e, tpl) {

    var question = e.currentTarget;

    // You can probably come up with something better here..
    var index = $(question).parent().find('> .question').index(question);

    Session.set('shouldIncludeExtra' + index, true);
  }
});

由于反应性,当您触发点击事件时,您会立即看到插入。

我意识到这并不能真正回答您问题的标题,但它应该可以为您带来想要的结果。

【讨论】:

  • 感谢您抽出宝贵的时间来回答,是的,会议总是为您提供救援 :) 什么新索引 @each helper 您有更多信息吗? Meteor 有一个问题,您无法从数组中获取索引。但这不是我可以使用_id的问题。我仍然想得到Blaze.render 的答案,这似乎是一个巨大的限制,我希望meteor 可以更灵活地使用它的参数。或者可能有一种我没有想到的方法。
  • 哦,是的,它刚刚出来,检查一下:github.com/meteor/meteor/pull/3560 我希望您可以将它作为参数传递给帮助程序,或者很可能它已经在数据上下文中可用。
猜你喜欢
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多