Route to the new data submitted by Meteor autoform using iron router?
Asked Answered
C

3

8

I'm using Meteor with AutoForm & Iron Router.

I have an autoform for inserting a data, and I want to redirect to the page of the data I added after a successful insert. How should I do it?

Here is the For:

{{#autoForm collection="Products" id="add" type="insert"}}
    <h4 class="ui dividing header">Products Information</h4>
      {{> afQuickField name='name'}}
      {{> afQuickField name='info'}}
    <button type="submit" class="ui button">Insert</button>
{{/autoForm}}

Iron Router:

Router.route('/products/:_id', {
  name: 'page',
  data: function() { return Products.findOne(this.params._id);}
});

Callbacks/Hooks

AutoForm.hooks({
  add: {
    onSuccess: function(doc) {
      Router.go('page', ???);
    }
  }
});
Continuo answered 27/4, 2015 at 18:54 Comment(1)
#26852378Bane
I
6

The AutoForm hook will return you the docId. See: https://github.com/aldeed/meteor-autoform#callbackshooks

this.docId: The _id attribute of the doc attached to the form, if there is one, or for an type='insert' form, the _id of the newly inserted doc, if one has been inserted.

So use:

Router.go('page',{_id: this.docId});
Idell answered 27/4, 2015 at 19:46 Comment(0)
B
0

According to the doc on github, signatures changed: don't forget to declare the forms or null to apply the hooks.

for all forms

AutoForm.addHooks(null,{
    onSuccess: function(formType, result) {
        Router.go('page',{_id: this.docId});
    }
});

for specific form

AutoForm.addHooks(['yourForm'],{
    onSuccess: function(formType, result) {
        Router.go('page',{_id: this.docId});
    }
});

Best is to check the up to date signatures: https://github.com/aldeed/meteor-autoform#callbackshooks

Burtie answered 14/1, 2016 at 15:59 Comment(0)
O
0
onSuccess: function(formType, result) {
    Router.go(
        ['adminDashboard', result, 'Edit'].join(''), 
        {_id: this.docId}
    );
},
Othilie answered 3/2, 2018 at 6:21 Comment(1)
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Madame

© 2022 - 2024 — McMap. All rights reserved.