在Meteor中改变钩子中的反应变量值

Jam*_*een 6 javascript node.js meteor meteor-autoform

我有

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar;
  this.variableName.set(true);
});
Run Code Online (Sandbox Code Playgroud)

templateName我有一个autoform.我需要设置无功变量variableNamefalse的时候autoform提交.

我试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.variableName.set(false);
    },
  }
});
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为this.它没有templateName像帮助器和事件那样引用模板.如果我使用会话,它会起作用,因为它们不限于特定模板.

如何更改autoform hook中的反应变量?

我也试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.template.variableName.set(false);
      this.template.parent.variableName.set(false);
      this.template.parent().variableName.set(false);
      this.template.parentData.variableName.set(false);
      this.template.parentData().variableName.set(false);
      this.template.parentView.variableName.set(false);
      this.template.parentView().variableName.set(false);
    },
  }
});
Run Code Online (Sandbox Code Playgroud)

使用console.log(this.template)它时会打印一个对象.如果我使用console.log(this.template.data)我得到

Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}
Run Code Online (Sandbox Code Playgroud)

我使用反应变量variableName来确定是显示可编辑的表单还是显示用户的数据的良好表示.也许有另一种更好的方法来做到这一点.

Zac*_*ltz 1

onCreated 的编辑:

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar(false);
});
Run Code Online (Sandbox Code Playgroud)

您可能需要添加一个辅助函数来获取 Template 实例:

Template.templateName.helpers({
    getVariableName: function() {
      return Template.instance().variableName.get();
    }
});
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的表单逻辑中调用

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      Template.getVariableName.set(false);
    },
  }
});
Run Code Online (Sandbox Code Playgroud)

MeteorChef 有一篇关于反应变量/字典反应变量的精彩文章。