2015-09-24 37 views
0

我有一個jQuery的AJAX功能是這樣的:如何擴展JQuery的ajax done()方法?

jQuery.ajax({ 
      url : '/blabla', 
      method : 'post', 
      data: { 
       bla : bla 
      } 
     }).done(function(data) { 
      // do lots of stuff 
     }); 

..我希望能夠增加一個檢查傳入完成回調函數的數據沒有在它session_timed_out值。假設我有許多與上面類似的功能,但他們都做不同的事情,但他們都需要檢查會話是否超時。有沒有適當的方法來擴展done(),以便它最初檢查超時?我試圖做這樣的事情,但它失敗:

var myAjax = function(options,callback){ 
    var defaults = {    
     done: function(data){ //hijack the success handler? 
      if(check(data)){  
       callback(data); 
      } 
     } 
    }; 
    jQuery.extend(options,defaults); 
    return jQuery.ajax(options); 
} 

當我使用此之前的作品一樣,這意味着支票不會被調用,因爲它似乎是由()完成回調在實際取代擴展功能實現,我認爲是有道理的。所以我想知道是否有一種方法來「裝飾」或擴展done()函數,以便首先檢查會話超時。或者我需要手動添加這個相同的會話檢查到我所有的ajax完成?

+0

僅供參考我上面實現的方法是:HTTP:// stackoverflow.com/questions/9815058/extending-jquery-ajax-success-globally –

+0

jquery具有全局處理程序 - 請參閱http://api.jquery.com/ajaxComplete/我已將它們用於類似的身份驗證案例。這不是裝飾,而是允許攔截。 – user2864740

+0

看看這個問題:http://stackoverflow.com/questions/12487794/overriding-jquery-functions-best-practice –

回答

3

這段代碼覆蓋了jQuery ajax方法,因此您可以在成功返回時添加額外的檢查。

(function($) { 

    var yourCustomCheck = function(ajaxRes) { 
     // Do whatever you need and return a boolean 
    }; 

    var oldAjax = $.ajax; 
    $.ajax = function(opts) { 
     return $.Deferred(function() { 
      var _def = this; 

      oldAjax.call(this, opts).done(function(res) { 
       console.log("this is done first"); 
       if(yourCustomCheck.call(this, res)) _def.resolve(res); 
       else _def.reject("timeout"); 
      }).fail(function() { 
       _def.reject(); 
      }); 
     }) 
    } 

})(jQuery); 

在此之後,你可以使用$.ajax()正常..

$.ajax({ 
    ..... 
}).done(function(res) { 
    console.log("ok"); 
}).fail(function() { 
    console.log("no ok"); 
}); 

這裏是工作示例的jsfiddle:https://jsfiddle.net/jormaechea/kffyo7qL/1/

+0

我會看看我是否可以使這個工作。目前,當我這樣做:console.log(ajaxRes);在yourCustomCheck()方法中 - 我沒有得到ajax響應,我得到了當前頁面的html。我會看看我能否修復它。 –

+0

剛剛修改這個以解決你所報告的問題..請再次檢查並讓我知道.. –

+0

對不起Juaquin。當我在控制檯登錄ajaxRes變種時,我仍然得到我所在頁面的html。我感謝你的努力,並會爲此讚賞你。 –

1

你可以鏈接一個超時檢查:

jQuery.ajax({ 
     url : '/blabla', 
     method : 'post', 
     data: { 
      bla : bla 
     } 
    }).then(timeoutCheck).then(function(data) { 
     // do lots of stuff 
    }, function(err) { 
     // handle error 
    }); 

    function timeoutCheck(data) { 
     if (check(data)) { 
      return data; 
     } else { 
      // return a rejected promise to turn fulfilled into reject 
      return jQuery.Deferred.reject(new Error("timeout")); 
     } 
    } 

或者,你可以把這個在自己的Ajax調用。

jQuery.ajaxT = function() { 
    return jQuery.ajax.apply(jQuery, arguments).then(timeoutCheck); 
} 

jQuery.ajaxT(...).then(function(results) { 
    // handle returned data here 
    // the timeoutCheck has already been done 
}, function(err) { 
    // handle any errors here 
}); 

然後,任何Ajax調用你jQuery.ajaxT()開始將自動地有timeoutCheck添加到它的承諾邏輯。如果ajax調用成功並且超時檢查通過,那麼該承諾將被履行。如果ajax調用成功並且超時檢查失敗,則承諾被拒絕。

+0

@ user2864740 - 是的,但它比OP所做的更乾淨,它提供了一個通道在任何給定請求上進行適當的錯誤處理。這是一個額外的'.then(timeoutCheck)'你希望這個檢查運行的ajax調用。人們可以用自定義包裝函數包裝'jQuery.ajax()',並在那裏做,然後當你想要這個額外的代碼時調用自定義包裝函數。 – jfriend00

+0

@ user2864740 - 我添加了一個包含內置功能的函數包裝。 – jfriend00