在meteor.js中,如何使用AutoForm.addHooks的onSuccess函数中的模板参数获取模板数据上下文?
我附加文档作为addCrop模板的数据上下文。当其中的自动表单成功提交时,我希望在此数据上下文中获取_id。我假设我可以从模板参数得到它。但是,我不知道怎么做在meteor.js中,如何使用AutoForm.addHooks的onSuccess函数中的模板参数获取模板数据上下文?,meteor,meteor-autoform,Meteor,Meteor Autoform,我附加文档作为addCrop模板的数据上下文。当其中的自动表单成功提交时,我希望在此数据上下文中获取_id。我假设我可以从模板参数得到它。但是,我不知道怎么做 AutoForm.addHooks(['addCrop'], { onSuccess: function(operation, result, template) { var _id = template.???? Router.go("cropEdit", {_id: _id});
AutoForm.addHooks(['addCrop'], {
onSuccess: function(operation, result, template) {
var _id = template.????
Router.go("cropEdit", {_id: _id});
}
});
Autoform文档中的onSuccess函数如下所示:
onSuccess: function(formType, result) {}
如果要在路由中设置数据上下文,则可以使用模板帮助器获取所需内容
创建模板帮助器:
Template.yourTemplate.helpers({
getRouteContext: function(){
return yourObject;
}
});
在自动表单中,将函数作为属性添加到表单:
{{#autoForm ... routeContext=getRouteContext}}
现在,您可以在钩子中访问它:
AutoForm.addHooks(['addCrop'], {
onSuccess: function(operation, result) {
console.log(this.formAttributes.routeContext);
} });
这似乎对我有用:
AutoForm.addHooks(['addCrop'], {
onSuccess: function(operation, result) {
var _id = this.template.id
Router.go("cropEdit", {_id: _id});
}
});
也就是说,注意在
onSuccess
处理程序中,this.template
是如何存在的。我最近回答的一个问题可能重复:不,它不同。您的答案是针对新插入的文档。实际上,result参数的值与this.docId相同。我为addCrop路由分配了一个数据上下文。插入新的“裁剪”后,您希望获取数据上下文的文档id,而不是新插入的裁剪的\u id;非常感谢你!这很有帮助。