为什么要在变量中保存验证器代码

时间:2015-10-09 00:32:00

标签: jquery meteor

我正在执行以下tutorial但是没有真正理解为什么我需要将验证器代码保存在变量中。 是因为我稍后使用validator.showErrors引用它吗?

我不能只使用submitHandler中的event参数吗?

这是代码。

Template.login.onRendered(function ( ){
    var validator = $(".login").validate({
        submitHandler: function (event) {
            var email = $("[name=email]").val();
            var password = $("[name=password]").val();
            Meteor.loginWithPassword(email, password, function(error) {
                if (error) {
                    if (error.reason == "User not found") {
                        validator.showErrors({
                            email: "That email doesn't belong to a registered user."
                        });
                    }
                    if (error.reason == "Incorrect password") {
                        validator.showErrors({
                            password: "You entered an incorrect password"
                        });
                    }
                } else {
                    var currentRoute = Router.current().route.getName();
                    if (currentRoute == "login") {
                        Router.go("home");
                    }
                }
            });
        }
    });
})

1 个答案:

答案 0 :(得分:1)

更正,因为您在validator.showErrors的回调中引用了Meteor.loginWithPassword。其中一些只是jQuery验证插件的工作方式。例如,aldeed:autoform具有完全不同的验证模式。