简体   繁体   English

使用requirejs加载jQuery验证

[英]loading jquery validation using requirejs

I am struggling with requirejs . 我在用requirejs挣扎。 I have following jquery validation custom addon module defined: 我定义了以下jquery验证自定义插件模块:

validation-addon.js 验证-addon.js

define(['jquery', 'jquery.validate', 'jquery.validate.unobtrusive'], 

function ($) {

    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function (element) {
            $(element).closest('.form-group').removeClass('has-error');
        }
    });

    ....
});

My requirejs config file : 我的requirejs配置文件:

config.js config.js

require.config({
    baseUrl: '/Scripts',
    paths: {
        'jquery': 'lib/jquery-2.0.3',
        'jquery.validate': 'lib/jquery.validate',
        'jquery.validate.unobtrusive': 'lib/jquery.validate.unobtrusive',
    },
    shim: {
        'jquery.validate': {
            deps: ['jquery']
        },
        'jquery.validate.unobtrusive': {
            deps: ['jquery.validate']
        }
    }
});

I am using these modules on my mvc user regitration view like this: 我在这样的mvc用户注册视图上使用这些模块:

register.cshtml register.cshtml

require(['/Scripts/modules/config.js'], function () {
    require(['modules/user-register', 'modules/validation-addon'], 

     function (userregistration) {
        //using user registration module here..
    }

  );
});

The problem i am facing is little weird. 我面临的问题有点奇怪。 When i go to user registration view first time and input wrong values in use input fields than custom highlight and unhighlight functions which i have overridden on jquery validator is not executing but if i reload the register view itself than it suddenly start executing. 当我第一次进入用户注册视图并在使用输入字段中输入错误值时,自定义突出显示和取消突出显示功能在jQuery验证程序上已被覆盖,但该功能未执行,但如果我重新加载了注册视图本身,它就会突然开始执行。

My feeling is that may be this is happening because of the loading sequence of modules, but i dont understand how to solve this, can anybody please help me to solve this issue ? 我的感觉是这可能是由于模块的加载顺序而发生的,但是我不明白如何解决此问题,有人可以帮我解决这个问题吗?

It looks like a timing issue. 看来是时间问题。 These kinds of issue sometimes clear when you do a reload because the order in which things initialize depends on what is in the cache, etc. 进行重新加载时,这类问题有时会很清楚,因为初始化的顺序取决于缓存中的内容,等等。

It is possible right now that jquery.validate.unobtrusive does its work on the DOM before the code that calls $.validator.setDefaults is executed. 现在有可能jquery.validate.unobtrusive在执行调用$.validator.setDefaults jquery.validate.unobtrusive的代码之前在DOM上进行工作。 To check whether $.validator.setDefaults is executed before jquery.validate.unobtrusive , remove the jquery.validate.unobtrusive requirement from validation-addon.js and add it to your main file. 要检查$.validator.setDefaults jquery.validate.unobtrusive是否在jquery.validate.unobtrusive之前执行,请从validation-addon.js删除jquery.validate.unobtrusive要求并将其添加到您的主文件中。

Change validation-addon.js to: validation-addon.js更改为:

define(['jquery', 'jquery.validate'], 

function ($) {

    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function (element) {
            $(element).closest('.form-group').removeClass('has-error');
        }
    });

    ....
});

And change register.cshtml to: 并将register.cshtml更改为:

require(['/Scripts/modules/config.js'], function () {
    require(['modules/user-register', 'modules/validation-addon', 'jquery.validate.unobtrusive'], 

     function (userregistration) {
        //using user registration module here..
    }

  );
});

If this solves your problem you probably don't want to keep the code like this but this is a quick and easy way to check whether this is the issue. 如果这解决了您的问题,则您可能不想保留这样的代码,但这是一种快速简便的方法来检查这是否是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM