通过模板传递方法参数

时间:2016-01-20 09:23:20

标签: javascript templates meteor simple-schema

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

从广义上讲,我必须通过奖励"参数作为参数。问题是,尽管我已经配置了奖项,但我找不到通过模板将其传递给方法的方法。我试过的一种方法是做一个帮手并尝试传递它:

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

但它返回错误:

  找不到方法"

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

1 个答案:

答案 0 :(得分:1)

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

“将使用您在meteormethod属性中指定的名称调用服务器方法。传递一个参数doc,即表单提交产生的​​文档。”

因此,使用基于方法的表单不会对您有所帮助。相反,使用“正常”形式:

{{#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;
    }
  }
});
相关问题