我知道这是一个老问题,但我只需要修复旧项目上的一些错误,并且遇到这种补丁的问题。
最好通过 options 对象使该功能可用,然后将您的特定逻辑放在那里。
修补:
(function monkeyPatchJQueryAutocomplete($) {
/**
* Proxies a private
* prototype method to the
* options Object
*
* @param {Object} obj
* @param {String} funcName
*/
function proxyPrivateMethodToOptions(obj, funcName) {
var __super = obj.prototype[funcName];
obj.prototype[funcName] = function() {
if (this.options[funcName]) {
return this.options[funcName].apply(this, arguments);
}
return __super.apply(this, arguments);
};
}
// Make the private _renderItem
// method available through the options Object
proxyPrivateMethodToOptions($.ui.autocomplete, '_renderItem');
// We can do this for other methods as well:
proxyPrivateMethodToOptions($.ui.autocomplete, '_renderMenu');
}($));
用法示例:
$('.some-input').autocomplete({
_renderItem: function(ul, item) {
console.log('here we can reference the old func via: ', __super);
return $("<li>")
.append($("<a>").text(item.label))
.appendTo(ul);
}
});