【问题标题】:Passing method parameters through template通过模板传递方法参数
【发布时间】:2016-04-26 01:30:42
【问题描述】:

我在流星中有以下方法(我使用模式),我调用它以便在数据库中插入一个对象。

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

但它返回一个错误:

“未找到方法”

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

【问题讨论】:

    标签: javascript templates meteor simple-schema


    【解决方案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;
        }
      }
    });
    

    【讨论】:

    • 感谢您的回答。实际上,我找到了另一种方法。我刚刚添加了一个隐藏字段,它的值是提到的助手:{{> afQuickField name='price' value=prizeRequest type="hidden"}}
    • 确实,这很简单:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2018-02-18
    • 2018-03-30
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多