Is there any way to insert a callback before/after a templates context is updated?

我只是一个虾纸丫 提交于 2019-12-11 19:06:19

问题


I'm aware of Template.onRendered, however I have the need to destroy and setup some plugins that act on the dom when the actual context is updated.

So saying I have content template, I'd need something similar to the following:

Template.content.onBeforeChange(function () {
  $(".editor").editable("destroy");
});

Template.content.onAfterChange(function () {
  $(".editor").editable();
});

Is there any current way I can achieve this with the existing Template api?


回答1:


You should be able to detect a context change within a template autorun by looking at currentData like this:

Template.content.onRendered(function() {
  this.autorun(function() {
    if (Template.currentData()) {
      // the context just changed - insert code here
    }
  });
});

I'm unclear if that works for your particular case because this technique only gets you the equivalent of onAfterChange.



来源:https://stackoverflow.com/questions/29418433/is-there-any-way-to-insert-a-callback-before-after-a-templates-context-is-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!