且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是JavaScript“代理模式"?

更新时间:2021-12-13 22:37:58

想象一下,您的站点中有许多ajax请求.设计有所变化.现在,在每个请求之前,您要显示一些自定义加载gif.您需要在有ajax请求的地方更改所有代码,也可以使用代理模式.

Imagine you have site with many ajax requests. There is a change in design. Now before each request you want to display some custom loading gif. You neeed to change all the code where there is an ajax request or you can use proxy pattern.

var proxied = jQuery.ajax; // Preserving original function
jQuery.ajax = function() { 
    jQuery("#loading").dialog({modal: true});
    return proxied.apply(this, arguments);
}