【问题标题】:Change value of reactive variable in hook in Meteor在 Meteor 的钩子中更改反应变量的值
【发布时间】:2015-10-05 18:31:33
【问题描述】:

我有

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar;
  this.variableName.set(true);
});

templateName 我有一个autoform。我需要在提交autoform 时将反应变量variableName 设置为false

我试过了

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      this.variableName.set(false);
    },
  }
});

但它不起作用,因为 this. 不像在助手和事件中那样引用模板 templateName。如果我使用会话代替它会起作用,因为这些不限于特定模板/范围。

我可以做些什么来更改自动表单挂钩中的反应变量?

我也试过

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);
    },
  }
});

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

Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}

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

【问题讨论】:

  • 我遇到了同样的情况,但我使用Sessionautorun 解决了它。并且onDestroyed 该模板我将Session 设为null...等待知道任何其他更好的方法来解决这个问题...赞一个!
  • @Jamgreen,Template.templateName.onCreatedAutoForm.hooks 代码是否在同一个文件中?
  • 你试过在你的钩子里使用Template.instance().variableName吗?
  • 还有console.log(Template.instance()); 打印null

标签: javascript node.js meteor meteor-autoform


【解决方案1】:

onCreated 的编辑:

Template.templateName.onCreated(function() {
  this.variableName = new ReactiveVar(false);
});

您可能想要添加一个辅助函数来获取模板实例:

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

然后您可能会在表单逻辑中调用

AutoForm.hooks({
  myForm: {
    onSuccess: function(operation, result) {
      Template.getVariableName.set(false);
    },
  }
});

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

【讨论】:

    【解决方案2】:

    如果人们偶然发现了这个解决方案,基于this thread,我可以在安装aldeed:template-extension 之后访问父级反应式字典/反应式变量,就像这样

    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);
        }   
      }
    });
    

    这里是 Html 和 JS 供参考。

    <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');
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多