Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard pattern to redirect a submit form after checking the insert was successful, on Meteor with AutoForm & Iron Router?

I'm using Meteor with AutoForm & Iron Router.

I have an autoform for inserting a record, and I want to redirect to another page to view the record after a successful insert. What's the generally accepted way to do this?

If I use the standard autoform insert like:

{{#autoForm collection="Articles" id="articleSubmit" type="insert"}} 

I can't see how I can redirect?

If I use the 'method' type like this:

{{#autoForm collection="Articles" id="articleSubmit" type="method"}} 

then I have to write an insert method which is not particularly DRY.

like image 957
Alveoli Avatar asked Mar 17 '23 12:03

Alveoli


1 Answers

A form is a form, if you use the type="method" thats means you are using a Meteor.method for this, and the form will handle for you, the Meteor.call

Now if you want to do some Router.go(), you will need to write some JS code, you can use the hooks, wich come with the autoform package, like this for example

Articles.hooks({
  contactForm: {
    onSubmit: function (insertDoc, updateDoc, currentDoc) {
      if (someHandler(insertDoc)) {
        this.done();
        Articles.clean(doc); / you can do more logic here, cleaning the form.
        Router.go('thePath');
      } else {
        this.done(new Error("Submission failed"));
      }
      return false;
    }
  }
});

So you don't need a common 'submit #articleSubmit' use the auto forms API better.

like image 81
Ethaan Avatar answered Apr 09 '23 15:04

Ethaan