Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display success message using autoform in meteor?

Tags:

meteor

I am using https://github.com/aldeed/meteor-autoform for one of my meteor collections. I am using quickForm and type insert. Here is the relevant code:

<template name="postInsert">
   <legend>Add your story here</legend>
   {{> quickForm collection="Posts" id="insertPostForm" type="insert" buttonContent="Post!" resetOnSuccess=true}}
</template>

This form submits and creates the post successfully. But it doesn't display a success message. I know I can use the onSuccess hooks and write my own success message. But I was wondering if there is a standard way to display success message using an autoform configuration?

I looked through the documentation on github and searched a bit, but all solutions point to using the onSuccess hooks. Any pointers here are appreciated

like image 455
Anurag Phadke Avatar asked Oct 10 '14 03:10

Anurag Phadke


2 Answers

After extensive search it turns out, onSuccess hooks ARE the standard way to display a success message. Here is my implementation of the same for completeness and for anyone else who might stumble upon this question in the future.

NEW Autoform 6.0.0

onSuccess: function(formType, result) {
    FlashMessages.sendSuccess('Success!');
    Router.go("/posts");
},

OLD

AutoForm.addHooks(['postInsert', 'postUpdate'], {
  onSuccess: function(operation, result, template) {
    FlashMessages.sendSuccess('Success!');
    Router.go("/posts");
  }
});

The use of AutoForm.addHooks keeps the code DRY allowing reuse for update as well as insert operations.

Also I am using the excellent flash-messages to display all my user messages. Highly recommended.

like image 193
Anurag Phadke Avatar answered Jan 03 '23 16:01

Anurag Phadke


I don't have enough reputation to comment, but according to the documentation, it seems Autoform.addHooks now takes the formId.

So you would bind your autoform with id 'insertPostForm' using

AutoForm.addHooks(['insertPostForm'], {
     onSuccess: function (operation, result, template) {
              ...
     }
});
like image 22
DFish Avatar answered Jan 03 '23 18:01

DFish