1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-17 05:18:32 +01:00

Add bindToXHR method

As alternative to the jQuery version.
This commit is contained in:
Barry vd. Heuvel 2014-10-28 10:48:42 +01:00
parent eb3f0d448a
commit fb364c73c3
2 changed files with 45 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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));
};
}
});