Search code examples
javascripttemplatesmeteorsimple-schema

Passing method parameters through template


I have the following method in meteor (I use schemas) which I call in order to insert an object in the database.

userAddOrder: function(newOrder, prize) {
        var currentPrize;
        if (prize == undefined) {
            currentPrize = undefined;
        }
        else{
            currentPrize = prize;
        }
        // Ininitalize the newOrder fields.
        // Check if someone is logged in
        if(this.userId) {
            newOrder.userId = this.userId;
            // Set the weight and price to be processed by the admin in the future
            newOrder.weight = undefined;
            newOrder.price = currentPrize;
            newOrder.status = false;
            newOrder.receiveDate = new Date();
            newOrder.deliveryDate = new Date();
            Orders.insert(newOrder);
        } else {
            return;
        }
    }, 

Broadly speaking, I have to pass it a "prize" parameter as a parameter. The problem is that despite the fact that I have the prize configured I could not find a way to pass it to the method through the template. One way I tried is to make a helper and try to pass it:

{{#autoForm schema="UserOrderSchema" id="userInsertOrderForm" type="method" meteormethod="userAddOrder,prizeRequest"}}  

But it returns an error:

"method not found"

Another way is to call the method in the js file by using a simple form(not the provided autoform). I think the second should work but I do not want to rewrite the whole Template. Is there a way to do it without it?


Solution

  • As stated in the auto form docs, the method has to take one parameter:

    "Will call the server method with the name you specify in the meteormethod attribute. Passes a single argument, doc, which is the document resulting from the form submission."

    So using a method based form isn't going to help you. Instead, use a 'normal' form:

    {{#autoForm schema="UserOrderSchema" id="userInsertOrderForm" type="normal"}} 
    

    Then, add an auto form submit hook:

    AutoForm.hooks({
      userInsertOrderForm: {
        onSubmit: function (insertDoc, updateDoc, currentDoc) {
          var prize = ...;
          Meteor.call('userAddOrder', prize, function(err, result) {
             if (!err) {
                this.done();
             } else {
               this.done(new Error("Submission failed"));
             });
          });
    
          return false;
        }
      }
    });