From fb364c73c31122c7c71933cef3ea8fabce20b107 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 28 Oct 2014 10:48:42 +0100 Subject: [PATCH] Add bindToXHR method As alternative to the jQuery version. --- src/DebugBar/JavascriptRenderer.php | 27 ++++++++++++++++++++++++++- src/DebugBar/Resources/debugbar.js | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/DebugBar/JavascriptRenderer.php b/src/DebugBar/JavascriptRenderer.php index 1dc7b3f..8b234a8 100644 --- a/src/DebugBar/JavascriptRenderer.php +++ b/src/DebugBar/JavascriptRenderer.php @@ -69,6 +69,8 @@ class JavascriptRenderer protected $ajaxHandlerClass = 'PhpDebugBar.AjaxHandler'; protected $ajaxHandlerBindToJquery = true; + + protected $ajaxHandlerBindToXHR = false; protected $openHandlerClass = 'PhpDebugBar.OpenHandler'; @@ -446,6 +448,27 @@ class JavascriptRenderer return $this->ajaxHandlerBindToJquery; } + /** + * Sets whether to call bindToXHR() on the ajax handler + * + * @param boolean $bind + */ + public function setBindAjaxHandlerToXHR($bind = true) + { + $this->ajaxHandlerBindToXHR = $bind; + return $this; + } + + /** + * Checks whether bindToXHR() will be called on the ajax handler + * + * @return boolean + */ + public function isAjaxHandlerBoundToXHR() + { + return $this->ajaxHandlerBindToXHR; + } + /** * Sets the class name of the js open handler * @@ -817,7 +840,9 @@ class JavascriptRenderer if ($this->ajaxHandlerClass) { $js .= sprintf("%s.ajaxHandler = new %s(%s);\n", $this->variableName, $this->ajaxHandlerClass, $this->variableName); - if ($this->ajaxHandlerBindToJquery) { + if ($this->ajaxHandlerBindToXHR) { + $js .= sprintf("%s.ajaxHandler.bindToXHR();\n", $this->variableName); + } else if ($this->ajaxHandlerBindToJquery) { $js .= sprintf("if (jQuery) %s.ajaxHandler.bindToJquery(jQuery);\n", $this->variableName); } } diff --git a/src/DebugBar/Resources/debugbar.js b/src/DebugBar/Resources/debugbar.js index 73bee1d..2d80839 100644 --- a/src/DebugBar/Resources/debugbar.js +++ b/src/DebugBar/Resources/debugbar.js @@ -1078,6 +1078,25 @@ if (typeof(PhpDebugBar) == 'undefined') { self.handle(xhr); } }); + }, + + /** + * Attaches an event listener to XMLHttpRequest + * + * @this {AjaxHandler} + */ + bindToXHR: function() { + var self = this; + var proxied = XMLHttpRequest.prototype.open; + XMLHttpRequest.prototype.open = function(method, url, async, user, pass) { + var xhr = this; + this.addEventListener("readystatechange", function() { + if (xhr.readyState == 4) { + self.handle(xhr); + } + }, false); + proxied.apply(this, Array.prototype.slice.call(arguments)); + }; } });