Javascript 流星钩中反应变量的变化值

Javascript 流星钩中反应变量的变化值,javascript,node.js,meteor,meteor-autoform,Javascript,Node.js,Meteor,Meteor Autoform,我有 在templateName中,我有一个autoform。提交自动表单时,我需要将反应变量variableName设置为false 我试过了 Template.templateName.onCreated(function() { this.variableName = new ReactiveVar; this.variableName.set(true); }); 但是它不起作用,因为这个。不像在帮助程序和事件中那样引用模板templateName。如果我改为使用会话,它会起作

我有

templateName
中,我有一个
autoform
。提交
自动表单
时,我需要将反应变量
variableName
设置为
false

我试过了

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar;
  this.variableName.set(true);
});
但是它不起作用,因为
这个。
不像在帮助程序和事件中那样引用模板
templateName
。如果我改为使用会话,它会起作用,因为这些会话不限于特定的模板

如何更改autoform钩子中的被动变量

我也试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.variableName.set(false);
    },
  }
});
当使用
console.log(this.template)
时,它会打印一个对象。如果我使用
console.log(this.template.data)

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);
    },
  }
});
我使用反应变量
variableName
来确定是否为用户显示可编辑表单或数据的良好表示。也许还有其他更好的方法可以做到这一点。

编辑onCreated:

Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}
您可能需要添加一个助手函数来获取模板实例:

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar(false);
});
然后,您可以在表单逻辑中调用

Template.templateName.helpers({
    getVariableName: function() {
      return Template.instance().variableName.get();
    }
});

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

如果人们偶然发现了一个解决方案,那么我可以在这样安装之后访问父反应Dict/反应变量

这里有Html和JS供参考

AutoForm.hooks({                                                                                                                         
  postFormId: {
    //onSuccess hook of my post form
    onSuccess: function(formType, result) {
      this.template.parent().reactiveDictVariable.set('imgId', 'newValueSetOnSuccess');
      //Or reactive var, and depending on your hierarchy
      //this.template.parent().parent().yourReactiveVar.set(undefined);
    }   
  }
});

{{{#autoForm schema=postFormSchema id=“postFormId”type=“method”method=“addPost”}
这是模板--{{imgId}中使用的反应变量
{{/autoForm}
Template.postsTemplate.created=function(){
//在这里使用反应式dict包
this.ReactiveDict变量=新的ReactiveDict();
this.reactiveDictVariable.set('imgId',未定义);
};
Template.posts.events(
“更改某些类”:函数(事件、模板){
//其他东西
template.postImgState.set('imgId','something');
}

我遇到了同样的情况,但我用
会话
自动运行
解决了它。并且
重新尝试了该模板
,我将
会话
设置为
…等待知道其他更好的方法来解决这个问题…砰的一声!@Jamgreen,是
模板。templateName。一旦创建了
Aut同一文件上的form.hooks
代码?是否尝试在钩子中使用
Template.instance().variableName
?还有
console.log(Template.instance());
打印
null
AutoForm.hooks({                                                                                                                         
  postFormId: {
    //onSuccess hook of my post form
    onSuccess: function(formType, result) {
      this.template.parent().reactiveDictVariable.set('imgId', 'newValueSetOnSuccess');
      //Or reactive var, and depending on your hierarchy
      //this.template.parent().parent().yourReactiveVar.set(undefined);
    }   
  }
});
<template name="postsTemplate"> 
  {{#autoForm schema=postFormSchema id="postFormId" type="method" meteormethod="addPost"}}
   <!-- other autoform stuff -->
   This is reactive variable used in template -- {{imgId}}
  {{/autoForm}}
</template>


Template.postsTemplate.created = function() { 
  //Using reactive-dict package here                                                                                                   
  this.reactiveDictVariable = new ReactiveDict();
  this.reactiveDictVariable.set('imgId', undefined);
};

Template.posts.events(
  "change .some-class": function(event, template) {
   //other stuff
   template.postImgState.set('imgId', 'something');
}