Skip to content

Commit

Permalink
Set ajax response type in initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed May 7, 2020
1 parent d00a3c6 commit caeba2a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/View/AjaxView.php
Expand Up @@ -16,10 +16,6 @@
*/
namespace Cake\View;

use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;

/**
* A view class that is used for AJAX responses.
* Currently only switches the default layout and sets the response type - which just maps to
Expand All @@ -33,23 +29,11 @@ class AjaxView extends View
protected $layout = 'ajax';

/**
* Constructor
*
* @param \Cake\Http\ServerRequest|null $request The request object.
* @param \Cake\Http\Response|null $response The response object.
* @param \Cake\Event\EventManager|null $eventManager Event manager object.
* @param array $viewOptions View options.
* @inheritDoc
*/
public function __construct(
?ServerRequest $request = null,
?Response $response = null,
?EventManager $eventManager = null,
array $viewOptions = []
) {
if ($response) {
$response = $response->withType('ajax');
}

parent::__construct($request, $response, $eventManager, $viewOptions);
public function initialize(): void
{
parent::initialize();
$this->setResponse($this->getResponse()->withType('ajax'));
}
}
Expand Up @@ -391,6 +391,7 @@ public function testAutoAjaxLayout(): void
$view = $this->Controller->createView();
$this->assertInstanceOf(AjaxView::class, $view);
$this->assertSame('ajax', $view->getLayout());
$this->assertSame((new Response(['type' => 'ajax']))->getType(), $view->getResponse()->getType());

$this->_init();
$this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'js'));
Expand Down

0 comments on commit caeba2a

Please sign in to comment.