Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在meteor.js中,如何使用AutoForm.addHooks的onSuccess函数中的模板参数获取模板数据上下文?_Meteor_Meteor Autoform - Fatal编程技术网

在meteor.js中,如何使用AutoForm.addHooks的onSuccess函数中的模板参数获取模板数据上下文?

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

我附加文档作为addCrop模板的数据上下文。当其中的自动表单成功提交时,我希望在此数据上下文中获取_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;非常感谢你!这很有帮助。