b24
b24

Reputation: 2473

get autoform non-collection forms method return value

I want to use meteor-autoform for my non-collection forms. I try this approach but I want to get method return value and display it on client. Please guide me how to do this.

This is my schema (common.js):

Schema = {};
Schema.echoSchema = new SimpleSchema({
    echoText: {
        type: String,
        label: "Echo Text",
        max: 50
    }
});

This is my code on the Client (client.js):

Template.showEcho.helpers({
    getEchoFormSchema: function() {
        return Schema.echoSchema;
    }
});

This is my code on the Server (server.js):

Meteor.methods({
  echoMethod: function (doc) {
    check(doc, Schema.echoSchema);
    return doc.echoText;
  },
});

This is my form template (showEcho.html):

    <template name="showEcho">
        {{#autoForm schema=getEchoFormSchema id="echoForm" type="method" meteormethod="echoMethod"}}
            <fieldset>
                <legend>Echo Form</legend>
                    {{> afQuickField name="echoText"}}
                    <div>
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <button type="reset" class="btn btn-default">Reset</button>
                    </div>
            </fieldset>
        {{/autoForm}}

        <p>
            // How To Show Echo Text HERE??
            Text = ???????????????????
        </p>
    </template>

Upvotes: 2

Views: 359

Answers (1)

bya
bya

Reputation: 331

Autoform hooks is your friend

Read about it here

Add this to your client code:

AutoForm.hooks({
    'echoForm': {
        after: {
           method: function(error, result) {
              console.log("after");
              if (result) {
                 return Session.set('result', result);
              }
           }
        }
     }
});

In your Template js file, create a helper to return Session.get 'result'

Template.showEcho.helpers({
  text: function() {
    return Session.get('result')

  }
});

Template html file:

<template name="showEcho">
    {{#autoForm schema=getEchoFormSchema id="echoForm" type="method" meteormethod="echoMethod"}}
       .
       .
       .
       . 
    {{/autoForm}}

    <p>
        {{text}}
    </p>
</template>

Upvotes: 1

Related Questions