protected function respond(array $response, $callback = NULL)
{
ee()->load->library('javascript');
if ($item_root_node = ee()->TMPL->fetch_param('item_root_node'))
{
$response_with_nodes = array();
foreach($response as $item)
{
$response_with_nodes[] = array($item_root_node => $item);
}
$response = $response_with_nodes;
}
if ($root_node = ee()->TMPL->fetch_param('root_node'))
{
$response = array($root_node => $response);
}
$response = function_exists('json_encode')
? json_encode($response)
: ee()->javascript->generate_json($response, TRUE);
if ( ! is_null($callback))
{
$response = call_user_func($callback, $response);
}
if ($this->check_xhr_required())
{
$response = '';
}
else if ($this->jsonp && $this->callback)
{
$response = sprintf('%s(%s)', $this->callback, $response);
}
if ($this->terminate)
{
@header('Content-Type: '.$this->content_type);
exit($response);
}
return $response;
}