【问题标题】:Is there any way to insert a callback before/after a templates context is updated?有没有办法在模板上下文更新之前/之后插入回调?
【发布时间】:2015-06-07 17:33:36
【问题描述】:

我知道 Template.onRendered,但是当实际上下文更新时,我需要销毁和设置一些作用于 dom 的插件。

所以说我有内容模板,我需要类似于以下内容:

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

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

目前有什么方法可以使用现有的模板 api 实现这一点吗?

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    您应该能够通过查看currentData 来检测template autorun 中的上下文更改,如下所示:

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

    我不清楚这是否适用于您的特定情况,因为这种技术只能让您获得相当于onAfterChange

    【讨论】:

    • 你是对的,虽然调查了一下,但似乎 blaze 模板系统在上下文更新之前不支持回调。所以这就是答案,谢谢:)
    猜你喜欢
    • 2015-02-13
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多