开发者

setValidationValues in jquery.validate.unobtrusive not available outside file?

开发者 https://www.devze.com 2023-02-06 09:40 出处:网络
I开发者_开发百科 am extending MVC 3 validation by using custom ValidationAttributes on the server side and have implemented IClientValidatable to output html5 data attributes for my custom validation.

I开发者_开发百科 am extending MVC 3 validation by using custom ValidationAttributes on the server side and have implemented IClientValidatable to output html5 data attributes for my custom validation. I then used jQuery.validator.addMethod to add my client side validation. My final task is to write the adapter that converts the html5 data attributes into a format that the jQuery validator understands. My validation takes in several values so I need to use the jQuery.validator.unobtrusive.adapters.add rather than one of the more straightforward functions such as addSingleVal.

Within my adapter, it would be useful to use several of the functions in the jquery.validate.unobtrusive file such as setValidationValues and getModelPrefix but if I am not mistaken, these function are inside a closure making them only available within that closure. I do not really want to put my custom functions in a microsoft owned script file that may change, so unless anyone has any ideas, I am forced to replicate these functions in my own file which is obviously not ideal.

So any ideas and what do other people do?


I am afraid not.
As you know these functions are implement like this:

(function ($) { 
   ...  
   function setValidationValues(options, ruleName, value){
      //the implementation 
   }
   ...  
}(jQuery))

as long as this function defined within a private scope you can not access it from out side, it kind of private function for a class. The purpose of defining these kind of functions is you may not access them outside of scope.
minifier methods are also aware of this FACT and use it to compress js as much as possible. if you look at the minified version jquery.validate.unobtrusive.min.js it prove itself that there is no longer a setValidationValues function but (some letter) x function.

It's not a big deal of copy and pasting for these function as long as they are too short to concern about their sizes. With minifier you can get this function and other 3 other function for just 200 bytes. and this 200 byte will be use only once.


However, there is a way if you use the minified version you can look up the name of functions and place it in your adapter.add(function(){//use their name here}), you would notice that copy and pasting is better.

0

精彩评论

暂无评论...
验证码 换一张
取 消