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.
onCreate
: Triggered whenever a requester object from theAjax
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. anAjax.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.