本文介绍了流星:使用Blaze更新集合后强制重新渲染整个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个其中更改了DOM的模板,并且当保存到数据库时我想重新呈现该模板.在Blaze之前,如果模板中某处有反应变量,Meteor会重新渲染整个模板,但是现在我该怎么做?

I have a template in which the DOM is changed, and I would like to rerender the template when saving to database. Before Blaze, Meteor would have rerendered the whole template if there was a reactive variable somewhere in the template, but now how can I do this ?

我在Iron路由器路由中设置了片段的集合:

I have a collection of clips set up in an Iron router route :

ClipsController = RouteController.extend({
    data: function() {
      clips = Clips.find({}, {sort: {created: 1}});
      return {clips: clips};
    }
});

还有剪辑的模板:

<template name="clips">
  {{#each clips}}
    {{> clip}}
  {{/each}}
</template>

然后,我有一个 clip 模板:

<template name="clip">
  <article class="clip" id="{{_id}}">
    {{{content}}}
    <ul class="tags">
      {{#each tags}}
        <li><a href="/#{{this}}">#{{this}}</a></li>
      {{/each}}
    </ul>
  </article>
</template>

以及该模板的脚本,该脚本可更改DOM,然后保存 clip :

And a script for this template which changes the DOM and then saves the clip :

Template.clip.events({
  'click .edit': function(event, template) {
    template.$('.tags li').each(function() {
      $(this).text($(this).text().replace(/^#(.*)/, "$1"));
    });
  },

  'click .save': function(event, template) {
    var data = {
      //...
    };

    Clips.update({_id: this._id}, data);

    // How to rerender the template ?
  }
});

推荐答案

我不认为Blaze提供了重新渲染整个模板的任何方法,因为Blaze的目的是进行精细的更新.

I don't believe that Blaze provides any way to rerender the entire template as the point of Blaze is to have fine grained updates.

一种快速而又肮脏的方法可能是使用Session,模板助手和包装整个模板的{{#unless}}块,然后在更新之前将Session密钥设置为true,然后将false设置为false导致{{#unless}}块中的所有内容都重新呈现.

A quick and dirty way to achieve this might be to use Session, a template helper, and an {{#unless}} block that wraps the whole template and then just set the Session key to true before the update and false after causing everything in the {{#unless}} block to rerender.

Template.clips.noRender = function(){
  return Session.get("noRender");
}

Template.clip.events({
  'click .edit': function(event, template) {
    template.$('.tags li').each(function() {
      $(this).text($(this).text().replace(/^#(.*)/, "$1"));
    });
  },

  'click .save': function(event, template) {
    var data = {
      //...
    };

    Session.set("noRender", true);

    Clips.update({_id: this._id}, data, function(){
      Session.set("noRender", false);
    });

    // How to rerender the template ?
  }
});

<template name="clips">
  {{#unless noRender}}
    {{#each clips}}
      {{> clip}}
    {{/each}}
  {{/unless}}
</template>

这篇关于流星:使用Blaze更新集合后强制重新渲染整个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:12