From 4fa078d709634f5a4c305ab54b7d3e1f0bcedf3f Mon Sep 17 00:00:00 2001 From: Anton Terekhov Date: Sat, 10 Nov 2012 16:06:19 +0400 Subject: [PATCH] Fixes for AjaxAction to send different header Content-type with $this->json_encode == true and $this->json_encode == false . This includes refactoring of $this->encode to $this->json_encode. --- app/AjaxAction.php | 29 +++++++++++++++++++---------- tests/app/AjaxActionTest.php | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/AjaxAction.php b/app/AjaxAction.php index c91affe..bab999e 100644 --- a/app/AjaxAction.php +++ b/app/AjaxAction.php @@ -3,22 +3,28 @@ * AjaxAction * * @copyright NetMonsters - * @link + * @link http://netmonsters.ru * @package Majestic - * @subpackage face - * @since - * @version SVN: $Id$ - * @filesource $URL$ + * @subpackage app + * @since 2011-04-27 */ /** - * базовый класс для всей экшенов выполняющихся по аякс-запросу + * Base class for all ajax Actions */ abstract class AjaxAction extends Action { + /** + * Data to output + * @var mixed + */ public $data = 1; - protected $encode = true; + /** + * Use json_encode + * @var bool + */ + protected $json_encode = true; function __construct() { @@ -28,10 +34,13 @@ abstract class AjaxAction extends Action function fetch() { - // header("Content-type: application/json; charset=utf-8"); - header("Content-type: text/html; charset=utf-8"); + if ($this->json_encode === true) { + header("Content-type: application/json; charset=utf-8"); + } else { + header("Content-type: text/html; charset=utf-8"); + } header("Cache-Control: no-store, no-cache, must-revalidate"); - $this->view->assign('data', $this->encode ? json_encode($this->data) : $this->data); + $this->view->assign('data', $this->json_encode ? json_encode($this->data) : $this->data); return $this->view->fetch($this->getTemplate()); } } diff --git a/tests/app/AjaxActionTest.php b/tests/app/AjaxActionTest.php index b8c7c82..f19583a 100644 --- a/tests/app/AjaxActionTest.php +++ b/tests/app/AjaxActionTest.php @@ -51,7 +51,7 @@ class AjaxActionTest extends Action_TestCase { Config::set('DEBUG', false); - Env::setParams(array('encode' => false)); + Env::setParams(array('json_encode' => false)); $controller = FrontController::getInstance(); $controller->setView('SomeView'); $action = $this->getMockForAbstractClass('AjaxAction' );