自动表单挂钩的 UniqueId

UniqueId for autoform hook

这可能是一个非常简单的答案,但是,我很困惑。我的网站上有很多表格。我想通知用户更新表单已保存。

在这个例子中,我创建了一个 uniqueId,它是一个问答表单,因此一页上可以有很多问题表单。我不确定如何将 uniqueId 与 AutoForm.addHooks(); 一起使用,我迄今为止尝试的所有内容都会创建全局自动表单挂钩,这并不理想,因为它会影响网站上的所有其他表单。

Template.answers.helpers({
  makeUniqueID: function () {
      return "update-each-" + this._id;
  },
});

var hooksObject = {
    onSuccess: function(formType, result) {
        var collectionId = this.docId;

        Meteor.call('sendEmail',
            collectionId
            );
    },
};

var answerForm = "update-each-" + this._id;
AutoForm.addHooks('answerForm', hooksObject);

更新

路径:answer.js

var hooksObject = {
  onSuccess: function(formType, result) {
    var collectionId = this.docId;

    Meteor.call('sendEmail',
      collectionId
      );
  },
};

Template.answers.onCreated(function() {
  var self = this;
    self.autorun(function() {
        var id = FlowRouter.getParam('id');
        self.subscribe('answers', id);
    });
    this.formId = "update-each-" + this._id;
    console.log("oncreated: ", this.formId);

    AutoForm.addHooks(this.formId, hooksObject, true); 
});



Template.answers.helpers({
  answer: function() {
    return Answers.find({"userId": Meteor.userId()});
  },
  formId() {
   /**
    * expose the form id we set above to the template
    **/
      var test = Template.instance().formId;
      console.log("test: ", test);
      return Template.instance().formId;
   },
});

路径:answer.html

{{#each answer}}
  {{#autoForm id=formId type="update" collection="Answers" doc=this}}
  {{> afQuickField name='answer'}}
    <button type="submit">Save changes</button>
  {{/autoForm}}
{{/each}}

根据更新的 post 和评论反馈中的新信息更新。 此解决方案的关键是在模板的 onCreated 事件中调用 AutoForm.addHooks。否则,您将无法访问唯一 ID:

answers.html

<template name="answers">
  {{#each answer}}
    {{> answerForm this }}
  {{/each}}
</template>

answers.js

Template.answers.onCreated(function() {
  this.autorun(() => {
    var id = FlowRouter.getParam('id');
    this.subscribe('answers', id);
  });      
});

Template.answers.helpers({
  answer: function() {
    return Answers.find({"userId": Meteor.userId()});
  }
});

answer.html

<template name="answerForm">
  {{#autoForm id=formId type="update" collection="Answers" doc=this}}
    {{> afQuickField name='answer'}}
    <button type="submit">Save changes</button>
  {{/autoForm}}
</template>

answer.js

Template.answerForm.onCreated(function onCreated() {
  /**
   * The data._id field is passed in to your template as data
   **/
  this.formId = "update-each-" + this.data._id;

  /**
   * Call addHooks with the third option, true, to replace any existing hooks for this form.
   * This is because addHooks will run every time an instance of this template is created.
   **/
  AutoForm.addHooks(this.formId, hooksObject, true);

});

Template.answerForm.helpers({
  formId() {
    /**
     * expose the form id we set above to the template
     **/
    return Template.instance().formId;
  }
});