Ajax.Responders

A repository of global listeners notified about every step of Prototype-based Ajax requests
有時候,你須要提供全部的Ajax操做發生頁面上經過的Ajax.Request,Ajax.Updater的或Ajax.PeriodicalUpdater通用行爲。
例如,您可能但願自動顯示指標正在進行時,一個Ajax請求,並隱藏它時都不是。你可能要分解出的異常處理以及在登陸頁面上自定義時尚的某處。有無數的可能性。
爲了實現這一目標, prototype提供Ajax.Responders,它能夠讓你註冊(而且,若是您願意,後來註銷)的反應,這是具備特別的命名方法的對象。這些名字來自一組對應於不一樣時間點(或成果)的一個Ajax請求的生命週期通常回調。
例如,原型自動註冊,保持一個漂亮的變量:Ajax.activeRequestCount一個響應。這意味着,在特定的時間,目前活躍的Ajax請求的數量 - 經過監測他們的onCreate的onComplete事件。此代碼是至關簡單:
Ajax.Responders.register({   onCreate: function() {     Ajax.activeRequestCount++;   },   onComplete: function() {     Ajax.activeRequestCount--;   } });
Responder callbacks

The callbacks for responders are similar to the callbacks described in the Ajax, but take a different signature. They're invoked with three parameters: the requester object (i.e., the corresponding "instance" of Ajax.Request), the XMLHttpRequest object, and the result of evaluating the X-JSON response header, if any (can be null). They also execute in the context of the responder, bound to the this reference.ajax

  • onCreate: Triggered whenever a requester object from the Ajax namespace is created, after its parameters are adjusted and before its XHR connection is opened. This takes two arguments: the requester object and the underlying XHR object.
  • onUninitialized (Not guaranteed): Invoked just after the XHR object is created.
  • onLoading (Not guaranteed): Triggered when the underlying XHR object is being setup, and its connection opened.
  • onLoaded (Not guaranteed): Triggered once the underlying XHR object is setup, the connection is open, and it is ready to send its actual request.
  • onInteractive (Not guaranteed): Triggered whenever the requester receives a part of the response (but not the final part), should it be sent in several packets.
  • onException: Triggered whenever an XHR error arises. Has a custom signature: the first argument is the requester (i.e. an Ajax.Request instance), and the second is the exception object.
  • onComplete: Triggered at the very end of a request's life-cycle, after the request completes, status-specific callbacks are called, and possible automatic behaviors are processed. Guaranteed to run regardless of what happened during the request.
相關文章
相關標籤/搜索