通过模板传递方法参数

Passing method parameters through template

我在 meteor 中有以下方法(我使用模式),我调用它是为了在数据库中插入一个对象。

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;
        }
    }, 

一般来说,我必须给它传递一个"prize"参数作为参数。问题是,尽管我配置了奖品,但我找不到通过模板将其传递给方法的方法。我尝试的一种方法是制作一个助手并尝试通过它:

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

但它 returns 一个错误:

"method not found"

另一种方法是使用简单的表单(不是提供的自动表单)调用js文件中的方法。我认为第二个应该可以,但我不想重写整个模板。没有它有什么办法吗?

如自动表单文档中所述,该方法必须采用一个参数:

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

因此,使用基于方法的表单对您没有帮助。相反,使用 'normal' 形式:

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

然后,添加一个自动表单提交钩子:

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;
    }
  }
});