Meteor AutoForm - 是否可以手动调用onSuccess或onError挂钩?

时间:2014-06-26 02:30:47

标签: javascript javascript-events meteor

我有这个表格来检查用户的CAPTCHA输入。当CAPTCHA有效时,应发送电子邮件,否则应显示错误。

AutoForm.addHooks(["form1", "form2", "form3"], {

    onSubmit: function(doc) {

        Meteor.call('validateCaptcha', formData, function(error, result) {

            if (result.success == true) {
                Meteor.call('sendEmail', doc);
            } else {
                Recaptcha.reload();
                // call onError here
                console.log("Error!");
            }

        });
    },

    onSuccess: function(operation, result, template) {
        // display success, reset form status
        console.log("CAPTCHA Validation Success! Email Sent!");
    }, 

    onError: function(operation, error, template) {
        // display error, reset form status
        console.log("Error: CAPTCHA Validation failed!");
    }
}

现在我的代码问题是,当用户提交正确的CAPTCHA时,代码将发送电子邮件,但不会触发onSuccess()挂钩。

当用户提交错误的CAPTCHA时也是如此。它显示我的错误消息,但不会触发onError()挂钩。

有没有办法手动调用这些钩子?

1 个答案:

答案 0 :(得分:2)

根据AutoForm hooks documentation,成功或失败的操作会调用onSuccessonError,不包括onSubmit

// Called when any operation succeeds, where operation will be
// "insert", "update", "remove", or the method name.
onSuccess: function(operation, result, template) {}, 

// Called when any operation fails, where operation will be
// "validation", "insert", "update", "remove", or the method name.
onError: function(operation, error, template) {},

如果您需要处理onSubmit的成功/失败案例,则应直接在if (result.success === true) ... else代码中执行此操作。如果您想要更精细的控制,您还可以在每个操作的基础上处理after挂钩中的成功和错误情况。