简体   繁体   English

通过模板传递方法参数

[英]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. 广义地说,我必须将“ prize”参数作为参数传递给它。 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). 另一种方法是使用简单表单(而不是提供的自动表单)在js文件中调用方法。 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? 没有它,有办法吗?

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." “将使用您在meteormethod属性中指定的名称调用服务器方法。传递一个参数doc,这是表单提交产生的​​文档。”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM