【问题标题】:UniqueId for autoform hook自动生成挂钩的 UniqueId
【发布时间】:2016-08-11 23:12:17
【问题描述】:

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

在这种情况下,我正在创建一个 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}}

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    根据更新后的帖子和评论反馈中的新信息进行更新。 此解决方案的关键是在模板的 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;
      }
    });
    

    【讨论】:

    • 对不起。 this.formId = "update-each-" + this.data._id; 似乎没有为更新表单创建唯一 ID。我是不是误会了?
    • 这假定 _id 字段作为数据对象的一部分传递到您的模板中,例如,如果父模板具有对该表单的 Mongo 文档的引用 doc,则您可以像这样调用您的答案模板:{{&gt; answers doc}} doc 只需要是一个包含 _id 字段的对象,这是我在测试示例时使用的代码:Template.body.helpers({ doc() { return { _id: 'asd12312' }; } }); 您需要修改它以使用 @987654332 @您在原始示例中使用的字段。您没有显示检索该字段的位置。
    • 嗨,Nathan,感觉有点不好意思。我仍然很难过。我已经更新了我的代码以尝试实现你的建议(见上文),你介意指出我哪里出错了。真的感觉自己像个菜鸟。
    • 我根据您的输入更新了我的代码。我认为困惑来自我不知道您从哪里获得表单ID。是来自FlowRouter.getParam('id') 的网址吗?
    • 表单id是集合id。 FlowRouter.getParam('id') 是用户的 ID。它与表单无关。
    猜你喜欢
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2016-10-30
    • 2019-12-02
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多