通过模板传递方法参数
问题描述:
我在流星(我使用模式)中有以下方法,我为了在数据库中插入一个对象而调用它。通过模板传递方法参数
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"}}
但它返回一个错误:
"method not found"
另一种方法是使用一个简单的调用在js文件的方法形式(不是提供的autoform)。我认为第二个应该可以工作,但我不想重写整个模板。有没有办法做到这一点?
答
如汽车形式的文档说明,该方法具有取一个参数:
“会叫你在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”}} – StefanL19
确实,这很简单:) – tarmes