jquery修改日志
增加功能:为ajax请求增加回调函数:beforeSuccess(),其参数与success()相同,但在success()执行前执行修改版本:jquery-2.1.3.js1、在 ajax: function( url, options ) 中,源码line:7933-7935将以下代码:// Deferredsdeferred = jQuery.Deferred(),completeDeferred = jQuery.Callbacks("once memory"),
修改成:
// Deferredsdeferred = jQuery.Deferred(),beforeDeferred = jQuery.Callbacks("once memory"),//增加本行代码completeDeferred = jQuery.Callbacks("once memory"),2、在 ajax: function( url, options ) 中的jqXHR定义,源码line:8004-8012,在abort()方法后增加beforeSuccess()方法将以下代码:
// Cancel the requestabort: function( statusText ) { var finalText = statusText || strAbort; if ( transport ) { transport.abort( finalText ); } done( 0, finalText ); return this;}
修改成:
// Cancel the requestabort: function( statusText ) { var finalText = statusText || strAbort; if ( transport ) { transport.abort( finalText ); } done( 0, finalText ); return this;},replaceState: false, //增加本参数beforeSuccess: function (){//增加本方法}3、在 ajax: function( url, options ) 中,源码line:8015-8019行将以下代码:
// Attach deferredsdeferred.promise( jqXHR ).complete = completeDeferred.add;jqXHR.success = jqXHR.done;jqXHR.error = jqXHR.fail;
修改成:
// Attach deferredsdeferred.promise( jqXHR ).complete = completeDeferred.add;deferred.promise( jqXHR ).beforeSuccess = beforeDeferred.add;//增加本行代码jqXHR.success = jqXHR.done;jqXHR.error = jqXHR.fail;4、在 ajax: function( url, options ) 中,源码line:8134-8137将以下代码:
// Install callbacks on deferredsfor ( i in { success: 1, error: 1, complete: 1 } ) { jqXHR[ i ]( s[ i ] );}
修改成:
// Install callbacks on deferredsfor ( i in { beforeSuccess: 1, success: 1, error: 1, complete: 1 } ) { jqXHR[ i ]( s[ i ] );}5、在function done()方法中,大概line:8100-8300行, 源码为line:8174-8284 将以下代码:
// Success/Errorif ( isSuccess ) { deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] );} else { deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );}
修改成:
// Success/Errorif ( isSuccess ) { if(s.replaceSuccess){ s.replaceSuccess( success, statusText, jqXHR ); if(jqXHR.replaceState){ beforeDeferred.fireWith( callbackContext, [ success, statusText, jqXHR ] ); }else{ beforeDeferred.fireWith( callbackContext, [ success, statusText, jqXHR ] ); deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); } }else{ beforeDeferred.fireWith( callbackContext, [ success, statusText, jqXHR ] ); deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); }} else { deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] );}
6、在代码最后,增加$.ajaxSetup(),为了修改方便才放最后
将以下代码:return jQuery;}));
修改成:
return jQuery;}));$.ajaxSetup({ replaceSuccess: function(obj, status, xhr){ //返回xhr.replaceState=true不执行自定义的success(),返回xhr.replaceState=false执行完replaceSuccess()方法后就执行success();默认为false if($.type(obj.error_code) != 'undefined'){ //自定义处理 alert(obj.error); xhr.replaceState = false; } }, beforeSuccess: function(response, status, xhr){ }});