我开始做 jQuery 插件,所以我没有太多经验,我今天想做的事情是为我正在处理的插件创建一个自定义事件。
我有一个很好的例子来告诉你我想要做什么是引导工具提示插件,使用这个插件我可以做这样的事情:
$('.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);
我该怎么做?