Meteor: Forcing rerendering whole template after collection update with Blaze

寵の児 提交于 2019-11-29 06:57:24

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.

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 provides an easy way to do this:

var template = Template.instance();
var parentDom = /*where to put your Template*/;
Blaze.remove(template.view);
Blaze.render(Template.clips, parentDom);

What it does is it removes your invalid Template and renders a new one as child. http://docs.meteor.com/#/full/blaze_remove
http://docs.meteor.com/#/full/blaze_render

I think this might be a better solution also the meteor way.

../clips.js

Template.clips.onRendered(function(){

   this.autorun(function(){
     Template.currentData();
   });

});

template.autorun(runFunc)

You can use this.autorun from an onCreated or onRendered callback to reactively update the DOM or the template instance. You can use Template.currentData() inside of this callback to access reactive data context of the template instance.

http://docs.meteor.com/#/full/template_autorun

iron-router data action is reactive default.

   clips = Clips.find({}, {sort: {created: 1}});

replace to

  clips = Clips.find({}, {sort: {created: 1}}).fetch();

I think a better way is to use Tracker.afterFlush.

For example:

Tracker.autorun ->
    Tracker.afterFlush ->
        # DOM is updated, why don't you do something here?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!