重构代码:消除longbow.validate脚本警告信息

This commit is contained in:
Argo-MacBookPro 2018-09-28 09:14:53 +08:00
parent 43796875af
commit 8354b86aa1
1 changed files with 16 additions and 21 deletions

View File

@ -139,32 +139,27 @@
var $firstElement = null; var $firstElement = null;
this.$element.find(op.childClass + ':visible').not(op.ignoreClass).each(function () { this.$element.find(op.childClass + ':visible').not(op.ignoreClass).each(function () {
if (!that.validElement(this) && $firstElement == null) $firstElement = $(this); if (!that.validElement(this) && $firstElement === null) $firstElement = $(this);
}); });
if ($firstElement) $firstElement.tooltip('show'); if ($firstElement) $firstElement.tooltip('show');
return $firstElement == null; return $firstElement === null;
}; };
Validate.prototype.validElement = function (element) { Validate.prototype.validElement = function (element) {
var result = this.check(element); var result = this.check(element);
this.tooltip(element, result); this.tooltip(element, result);
return result return result;
}; };
Validate.prototype.tooltip = function (element, valid) { Validate.prototype.tooltip = function (element, valid) {
if (valid == "pending") return; if (valid === "pending") return;
var op = this.options; var op = this.options;
var $this = $(element); var $this = $(element);
try {
if (valid) $this.tooltip('dispose'); if (valid) $this.tooltip('dispose');
else { else {
if (!$this.hasClass(op.errorClass)) $this.tooltip(); if (!$this.hasClass(op.errorClass)) $this.tooltip();
} }
}
catch (e) {
}
if (!valid) { if (!valid) {
$this.removeClass(op.validClass).addClass(op.errorClass); $this.removeClass(op.validClass).addClass(op.errorClass);
} }
@ -194,7 +189,7 @@
}; };
Validate.prototype.defaultMessage = function (element, rule) { Validate.prototype.defaultMessage = function (element, rule) {
var message = $(element).attr('data-' + rule.method + '-msg') || (rule.method == 'required' && $(element).attr('placeholder')) || $.validator.messages[rule.method]; var message = $(element).attr('data-' + rule.method + '-msg') || rule.method === 'required' && $(element).attr('placeholder') || $.validator.messages[rule.method];
var theregex = /\$?\{(\d+)\}/g; var theregex = /\$?\{(\d+)\}/g;
if (typeof message === "function") { if (typeof message === "function") {
message = message.call(this, rule.parameters, element); message = message.call(this, rule.parameters, element);
@ -232,7 +227,7 @@
Validate.prototype.rules = function (element) { Validate.prototype.rules = function (element) {
var $this = $(element); var $this = $(element);
var rules = $this.data('lgb.Validate.Rules'); var rules = $this.data('lgb.Validate.Rules');
if (!rules) $this.data('lgb.Validate.Rules', (rules = $.validator.normalizeRules($.extend({ required: true }, $.validator.classRules(element), $.validator.attributeRules(element), this.attributeRules(element))))); if (!rules) $this.data('lgb.Validate.Rules', rules = $.validator.normalizeRules($.extend({ required: true }, $.validator.classRules(element), $.validator.attributeRules(element), this.attributeRules(element))));
return rules; return rules;
}; };
@ -240,13 +235,13 @@
return this.each(function () { return this.each(function () {
var $this = $(this); var $this = $(this);
var data = $this.data('lgb.Validate'); var data = $this.data('lgb.Validate');
var options = typeof option == 'object' && option; var options = typeof option === 'object' && option;
if (!data && /valid|defaults/.test(option)) return if (!data && /valid|defaults/.test(option)) return;
if (!data) $this.data('lgb.Validate', (data = new Validate(this, options))); if (!data) $this.data('lgb.Validate', data = new Validate(this, options));
if (typeof option == 'string') data[option](); if (typeof option === 'string') data[option]();
}) });
}; }
$.fn.lgbValidate = Plugin; $.fn.lgbValidate = Plugin;
$.fn.lgbValidate.Constructor = Validate; $.fn.lgbValidate.Constructor = Validate;
@ -255,7 +250,7 @@
}; };
$.fn.lgbValid = function () { $.fn.lgbValid = function () {
var $this = this; var $this = this;
return $this.attr(Validate.DEFAULTS.validResult) == 'true'; return $this.attr(Validate.DEFAULTS.validResult) === 'true';
}; };
$(function () { $(function () {