jQuery小部件内的重写方法 [英] Override method inside jquery widget
问题描述
我正在尝试覆盖jquery小部件内的方法.该方法可以在第122行的 https://github.com/got5/tapestry5-jquery/blob/master/src/main/resources/org/got5/tapestry5/jquery/validation.js
I'm trying to override a method inside a jquery widget. The method can be found on line 122 at https://github.com/got5/tapestry5-jquery/blob/master/src/main/resources/org/got5/tapestry5/jquery/validation.js
Id喜欢更改第141行的html输出
Id like to alter the html output on line 141
我尝试将以下内容添加到自定义js类中,但未成功.如果有人可以解释如何执行此操作,则id非常感谢.
I've tried adding the following to my custom js class without success. If anybody could explain how to do this, id greatly appreciate it.
(function($) {
$.widget( "ui.tapestryFieldEventManager", {
showValidationMessage : function(message) {
var field = this.element;
var form = field.closest('form');
this.options.validationError = true;
form.formEventManager("setValidationError", true);
field.addClass("t-error");
this.getLabel() && this.getLabel().addClass("t-error");
var icon = this.getIcon();
if (icon) icon.show();
alert("here");
var id = field.attr('id')+"\\:errorpopup";
if($("#"+id).size()==0) //if the errorpopup isn't on the page yet, we create it
field.after("<div id='"+field.attr('id')+":errorpopup' class='tjq-error-popup test'/>");
Tapestry.ErrorPopup.show($("#"+id),"<span>"+message+"</span>");
}
});
})(jQuery);
推荐答案
您必须在原型上覆盖它.
You have to override it on the prototype.
var oldMethod = $.ui.tapestryFieldEventManager.prototype.showValidationMessage;
$.ui.tapestryFieldEventManager.prototype.showValidationMessage = function (message) {
// do your stuff here
alert("worky");
// apply old method
oldMethod.apply(this,arguments);
};
当然,如果新方法完成了旧方法所做的所有工作,则可以跳过旧方法的应用.
Of course, you could skip applying the old method if your new method does everything that the old method did.
这篇关于jQuery小部件内的重写方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!