【问题标题】:Pass additional args in autoform method in Meteor在 Meteor 的 autoform 方法中传递额外的参数
【发布时间】:2015-10-04 21:10:57
【问题描述】:

我用我的快速表单插入文档

{{> quickForm id=id collection=collection type="method" meteormethod="createDoc"}}

但我也想在所有插入的文档上设置userId

我的模板中有变量userId,所以我想知道我是否可以这样做

{{> quickForm id=id collection=collection type="method" meteormethod="createDoc" userId=userId}}

并在我的服务器方法中使用传递的变量?

【问题讨论】:

    标签: javascript node.js meteor meteor-autoform


    【解决方案1】:

    您可以在 autoform 挂钩中添加其他参数:

    HTML

    {{> quickForm id="idForm" collection=collection type="method" meteormethod="createDoc"}}
    

    JS

    AutoForm.hooks({
            idForm: {
                before: {
                    method: function(doc) {
                        doc.user = {}
                        doc.user._id = Meteor.user()._id;
                        doc.user.username = Meteor.user().username;
                        return doc;
                    }               
                },
                onSuccess: function(formType, result) {             
                    console.log(formType, result);
                    this.resetForm();
                },
                onError: function(formType, error) {
                    console.log(formType, error)
                }
    
                }
            }
        }); 
    

    有关更多详细信息,请查看文档的此部分 Callbacks/Hooks

    【讨论】:

    • 谢谢。但是是否可以将模板中的变量放入钩子中?我不能只使用Meteor.user()
    • 在我的情况下,我在钩子中添加更多数据,其他选项是使用 this.userId; 在服务器端添加变量。
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多