Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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', ???);
    }
  }
});
like image 493
user1909176 Avatar asked Apr 27 '15 18:04

user1909176


1 Answers

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});
like image 97
Mark Leiber Avatar answered Oct 09 '22 04:10

Mark Leiber