这可能是一个非常简单的答案,但是,我很难过。我的网站上有很多表单。我想通知用户已保存更新表单。
在这种情况下,我创建了一个uniqueId,它是一个Q& A表单,因此在一个页面上可以有很多问题表单。我不确定如何将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}}
答案 0 :(得分:2)
根据更新的帖子和评论反馈中的新信息进行了更新。
此解决方案的关键是在模板的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;
}
});