Meteor:在使用 Blaze 更新集合后强制重新渲染整个模板 [英] Meteor: Forcing rerendering whole template after collection update with Blaze

查看:14
本文介绍了Meteor:在使用 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 的模板:

Then, I have a template for 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 然后保存剪辑:

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导致 {{#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>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆