mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-19 00:28:47 +02:00
JsonPage: Add option to output only the data, typically when feeding a third party lib that doesn't understand our structuration
This commit is contained in:
@@ -7,6 +7,11 @@
|
||||
class JsonPage extends WebPage
|
||||
{
|
||||
protected $aData = [];
|
||||
/**
|
||||
* @var bool If true, only static::$aData will be output; otherwise data and scripts will be output in a structured object.
|
||||
* This can be useful when feeding response to a third party lib that doesn't understand the structured format.
|
||||
*/
|
||||
protected $bOutputDataOnly = false;
|
||||
|
||||
/**
|
||||
* JsonPage constructor.
|
||||
@@ -48,6 +53,19 @@ class JsonPage extends WebPage
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see static::$bOutputDataOnly
|
||||
* @param bool $bFlag
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function SetOutputDataOnly(bool $bFlag)
|
||||
{
|
||||
$this->bOutputDataOnly = $bFlag;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function output()
|
||||
{
|
||||
$this->add_header('Content-type: application/json');
|
||||
@@ -58,7 +76,7 @@ class JsonPage extends WebPage
|
||||
|
||||
$aScripts = array_merge($this->a_init_scripts, $this->a_scripts, $this->a_ready_scripts);
|
||||
|
||||
$aJson = [
|
||||
$aJson = $this->bOutputDataOnly ? $this->aData : [
|
||||
'data' => $this->aData,
|
||||
'scripts' => $aScripts,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user