【发布时间】:2013-05-21 14:31:00
【问题描述】:
我开始做 jQuery 插件所以我没有太多经验,我今天想做的事情是为我正在开发的插件创建一个自定义事件。
bootstrap tooltip plugin 是一个很好的例子来告诉你我想要做什么,使用这个插件我可以做这样的事情:
$('.my-tooltip').tooltip('show');
强制插件做一个动作,本例执行show()插件函数
对于我的具体情况,我正在使用 jQuery 验证插件在网站周围自定义验证表单,我不想这样做:
$('form#whatever').validateForm('showInlineChecks');
强制插件对某些特定字段进行验证。
让我们看看我的代码:
/*
* jQuery plugin to validate forms around segurosdigitales
* requires jquery validate plugin
* */
if( typeof Object.create !== 'function' ){
Object.create = function( obj ) {
function F(){};
F.prototype = obj;
return new F();
}
}
(function($){
var validator = {
// init the plugin
init: function(options, element){
var self = this;
self.options = $.extend( {}, $.fn.validateForm.options, options );
self.elem = element;
self.$elem = $(element);
self.validateTheForm();
},
// Set default options for jQuery validate plugin
setValidationOptions: function(){
var self = this;
// Default options for all forms around the app
// These are the default options for jquery validate plugin
var jQueryValidatePluginOptions = {
onfocusout: function(e){
// Validate all elements when 'blur' event happens, except if it has a 'datepicker-open' class, I'm adding this class because datepicker causes that the input element loses focus and validation error is triggered
if(!$(e).hasClass('datepicker-open')){
this.element(e);
}
},
errorElement: 'span',
errorClass: 'error help-block',
errorPlacement: function(error, element){
if(element.is('input[type="checkbox"]')){
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
// Highlight occurs when element has erros
highlight: function(element, errorClass, validClass){
$(element).addClass(errorClass).removeClass(validClass);
var control_group = $(element).closest('.control-group, .controls-row');
control_group.addClass(errorClass).removeClass(validClass);
// Remove any valid icon
control_group.find('i.icon-ok').remove();
},
// Unhighlight occurs when element is valid
unhighlight: function(element, errorClass, validClass){
$(element).removeClass(errorClass).addClass(validClass);
// get the parent of the field
var control_group = $(element).closest('.control-group, .controls-row');
// is field empty?
var element_is_empty = ( $(element).val() === '' );
if (!element_is_empty && !control_group.find('i.icon-ok').length) {
var label = control_group.find('label');
// Prevent to add more than one icon if control group contains more than one label (ie. when we have checkboxes and radio buttons)
$.each(label, function () {
$(this).prepend('<i class="icon-ok green"></i>');
return false;
});
// add class only if element is valid and not empty
control_group.removeClass(errorClass).addClass(validClass);
}
}
};
// add other options depending of validateForm plugin options
if( self.options.errorMessages ){
jQueryValidatePluginOptions.messages = self.options.errorMessages;
}
if( self.options.rules ){
jQueryValidatePluginOptions.rules = self.options.rules;
}
if( self.options.showNotification ){
jQueryValidatePluginOptions.invalidHandler = function(event, validator){
var errors = validator.numberOfInvalids();
if(errors){
generateNotification('error', false, 'Por favor corrige los errores en los campos resaltados en rojo para poder continuar.');
}
}
}
return jQueryValidatePluginOptions;
},
// Validate form
validateTheForm: function(){
var self = this;
var validateOpts = self.setValidationOptions();
self.$elem.validate(validateOpts);
}
};
$.fn.validateForm = function(options){
return this.each(function(){
var validate = Object.create(validator);
validate.init(options, this);
});
};
$.fn.validateForm.options = {
errorMessages: null,
rules: null,
showNotification: true
}
})(jQuery);
我该怎么做?
【问题讨论】:
-
好吧,首先,你不想每次调用
validateForm时都初始化一个新的验证器。 -
谢谢@KevinB,那我该怎么办?
-
在元素上存储对已创建实例的引用,如果元素已经表示已创建实例,则调用传递给插件的第一个参数指定的方法,如果它是一个字符串。 (样品即将推出)
-
这是我所指内容的一个非常简化的版本:jsfiddle.net/NSEs8/1 请注意,我不是总是调用 init,而是仅在插件第一次运行时才调用 init。这里有更深入的版本:keith-wood.name/pluginFramework.html
-
我已经看到您在插件中所做的更改,感谢您向我解释和做小提琴。
标签: jquery jquery-plugins