Search code examples
javascriptmeteoriron-routermeteor-autoform

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.


Solution

  • 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.