0

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.

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result, template) {
        var _id = template.????
        Router.go("cropEdit", {_id: _id});
    }
});
Jerry Yuan
  • 685
  • 2
  • 6
  • 16
  • Possible duplicate of a question I answered recently: http://stackoverflow.com/questions/29903775/route-to-the-new-data-submitted-by-meteor-autoform-using-iron-router – Mark Leiber May 05 '15 at 14:50
  • No. It is different. Your answer is for the new inserted doc. Actually, the result parameter has the same value of this.docId. I assign a data context for the addCrop route. After I insert a new 'crop', you want to grab the doc id of data context rather than the newly inserted crop's _id; – Jerry Yuan May 05 '15 at 15:17

2 Answers2

0

The onSuccess function in the Autoform documentation looks like this:

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 ... routeContext=getRouteContext}}

Now you can access it in your hook:

AutoForm.addHooks(['addCrop'], {
        onSuccess: function(operation, result) {
        console.log(this.formAttributes.routeContext);
    } });
Mark Leiber
  • 3,118
  • 2
  • 13
  • 22
0

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.

Luke
  • 20,878
  • 35
  • 119
  • 178