2013-09-15 12:56:08 -04:00
|
|
|
# AJAX and Stacked data
|
|
|
|
|
|
|
|
## AJAX
|
|
|
|
|
|
|
|
As mentioned in the previous chapter, if you are performing AJAX requests
|
|
|
|
which return HTML content, you can use `JavascriptRenderer::render(false)`.
|
|
|
|
|
2014-01-16 21:41:41 +00:00
|
|
|
In the case you are sending back non-HTML data (eg: JSON), the DebugBar can
|
2013-09-15 12:56:08 -04:00
|
|
|
send data to the client using HTTP headers using the `sendDataInHeaders()` method
|
|
|
|
(no need to use the `JavascriptRenderer`):
|
|
|
|
|
|
|
|
$debugbar = new DebugBar();
|
|
|
|
// ...
|
|
|
|
$debugbar->sendDataInHeaders();
|
|
|
|
|
|
|
|
On the client side, an instance of `PhpDebugBar.AjaxHandler` will
|
|
|
|
parse the headers and add the dataset to the debugbar.
|
|
|
|
|
|
|
|
The AjaxHandler automatically binds to jQuery's *ajaxComplete* event
|
|
|
|
so if you are using jQuery, you have nothing to configure.
|
|
|
|
|
|
|
|
If you're not using jQuery, you can call `AjaxHandler.handle(xhr)`.
|
2014-01-16 21:41:41 +00:00
|
|
|
If you are using the `JavascriptRenderer` initialization, the instance
|
2013-09-15 12:56:08 -04:00
|
|
|
of `AjaxHandler` is stored in the `ajaxHandler` property of the `DebugBar` object.
|
|
|
|
|
|
|
|
debugbar.ajaxHandler.handle(xhr);
|
|
|
|
|
2013-09-23 10:41:54 -04:00
|
|
|
If you are sending a lot of data through headers, it may cause problems
|
2014-01-16 21:41:41 +00:00
|
|
|
with your browser. Instead you can use a storage handler (see Storage chapter)
|
|
|
|
and the open handler (see Open Handler chapter) to load the data after an ajax
|
2013-09-23 10:41:54 -04:00
|
|
|
request. Use true as the first argument of `sendDataInHeaders()`.
|
|
|
|
|
|
|
|
$debugbar = new DebugBar();
|
|
|
|
|
|
|
|
// define a storage
|
|
|
|
$debugbar->setStorage(new DebugBar\Storage\FileStorage('/path/to/storage'));
|
|
|
|
|
|
|
|
// define the open handler url
|
|
|
|
$renderer = $debugbar->getJavascriptRenderer();
|
|
|
|
$renderer->setOpenHandlerUrl('open.php');
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
$debugbar->sendDataInHeaders(true);
|
|
|
|
|
2013-09-15 12:56:08 -04:00
|
|
|
## Stacked data
|
|
|
|
|
|
|
|
Some times you need to collect data about a request but the page won't actually
|
|
|
|
be displayed. The best example of that is during a redirect. You can use the
|
|
|
|
debug bar storage mechanism to store the data and re-open it later but it can
|
|
|
|
be cumbersome while testing a redirect page.
|
|
|
|
|
2014-01-16 21:41:41 +00:00
|
|
|
The solution is to use stacked data. The debug bar can temporarily store the
|
2013-09-15 12:56:08 -04:00
|
|
|
collected data in the session until the next time it will be displayed.
|
|
|
|
Simply call `DebugBar::stackData()` instead of rendering the debug bar.
|
|
|
|
|
|
|
|
PHP's session must be started before using this feature.
|
|
|
|
|
2013-10-01 06:10:14 +02:00
|
|
|
Note: The stacked data feature will use the storage mechanism if it's enabled
|
2013-09-15 12:56:08 -04:00
|
|
|
instead of storing the data in the session.
|
|
|
|
|
|
|
|
$debugbar = new DebugBar();
|
|
|
|
// ...
|
|
|
|
$debugbar->stackData();
|
|
|
|
|
|
|
|
Stacked data are rendered each time the debug bar is rendered using the
|
2013-10-01 06:10:14 +02:00
|
|
|
`JavascriptRenderer`.
|