Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monkey-Patch: jquery-autocomplete.js (_renderItem & _renderMenu)
(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');
// @example
//
// $('.some-input').autocomplete({
// _renderItem: function(ul, item) {
// console.log(__super);
// return $("<li>")
// .append($("<a>").text(item.label))
// .appendTo(ul);
// }
// });
//
}($));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.