Estoy buscando una manera de expandir una función Jquery existente para darle más opciones / parámetros.
El que quiero usar es $ .ajax pero esto podría aplicarse a cualquier función jQuery.
Quiero poder llamar a una función como esta:
$.adv_ajax()
Que sería una versión extendida de la función $ .ajax como la siguiente:
$.adv_ajax({ custom_parameter: "abc", //my custom one url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); } });
Bueno, entonces simplemente adjunte su función al objeto jQuery:
$.adv_ajax = function(options) { // Do stuff like change some options and then call the original return $.ajax(options); }
Algo como esto:
(function($) { // maintain a to the existing function var oldAjax = $.ajax; // ...before overwriting the jQuery extension point $.fn.ajax = function() { // original behavior - use function.apply to preserve context var ret = oldAjax.apply(this, arguments); // preserve return value (probably the jQuery object...) return ret; }; })(jQuery);
(function($) { // maintain a to the existing function var oldAjax = $.ajax; // ...before overwriting the jQuery extension point $.fn.ajax = function() { // original behavior - use function.apply to preserve context var ret = oldAjax.apply(this, arguments); // preserve return value (probably the jQuery object...) return ret; }; })(jQuery);