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

时间:2022-06-12 17:07:27

I attach the document as the data context of addCrop template. When the autoform in it is submitted successfully, I want to get the _id in this data context. I am assuming that I can get it from template parameter. But, I do not know how to do it.

我将文档附加为addCrop模板的数据上下文。当成功提交autoform时,我想在此数据上下文中获取_id。我假设我可以从模板参数中获取它。但是,我不知道该怎么做。

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result, template) {
        var _id = template.????
        Router.go("cropEdit", {_id: _id});
    }
});

2 个解决方案

#1


The onSuccess function in the Autoform documentation looks like this:

Autoform文档中的onSuccess函数如下所示:

onSuccess: function(formType, result) {}

If you're setting the data context in your route, you can then use a template helper to get what you need.

如果您在路线中设置数据上下文,则可以使用模板助手来获取所需内容。

Create a template helper:

创建模板助手:

Template.yourTemplate.helpers({
    getRouteContext: function(){ 
      return yourObject;
    }
});

In your autoform, add the function as an attribute to the form:

在您的autoform中,将该函数作为属性添加到表单中:

{{#autoForm ... routeContext=getRouteContext}}

Now you can access it in your hook:

现在您可以在钩子中访问它:

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result) {
        console.log(this.formAttributes.routeContext);
    } });

#2


This seems to be working for me:

这似乎对我有用:

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result) {
        var _id = this.template.id
        Router.go("cropEdit", {_id: _id});
    }
});

I.e. Notice how within the onSuccess handler, the this.template exists.

即请注意,在onSuccess处理程序中,this.template是如何存在的。

#1


The onSuccess function in the Autoform documentation looks like this:

Autoform文档中的onSuccess函数如下所示:

onSuccess: function(formType, result) {}

If you're setting the data context in your route, you can then use a template helper to get what you need.

如果您在路线中设置数据上下文,则可以使用模板助手来获取所需内容。

Create a template helper:

创建模板助手:

Template.yourTemplate.helpers({
    getRouteContext: function(){ 
      return yourObject;
    }
});

In your autoform, add the function as an attribute to the form:

在您的autoform中,将该函数作为属性添加到表单中:

{{#autoForm ... routeContext=getRouteContext}}

Now you can access it in your hook:

现在您可以在钩子中访问它:

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result) {
        console.log(this.formAttributes.routeContext);
    } });

#2


This seems to be working for me:

这似乎对我有用:

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result) {
        var _id = this.template.id
        Router.go("cropEdit", {_id: _id});
    }
});

I.e. Notice how within the onSuccess handler, the this.template exists.

即请注意,在onSuccess处理程序中,this.template是如何存在的。