Skip to content

Commit

Permalink
Also allow success to go skip rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Jul 25, 2019
1 parent 2af8caf commit 030c75f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
28 changes: 28 additions & 0 deletions docs/Component/Ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ The nice bonus is the auto-fallback: The controller and all deleting works norma

A live example can be found in the [Sandbox](https://sandbox.dereuromark.de/sandbox/ajax-examples/table).

### Simple boolean response

In cases like "edit in place" you often just need a basic AJAX response as boolean YES/NO, maybe with an error message on top.
Since we ideally always return a 200 OK response, we need a different way of signaling the frontend if the operation was successful.

Here you can simplify it using the special "error"/"success" keys that auto-format the reponse as JSON:
```php
$this->request->allowMethod('post');

$value = $this->request->getData('value');
if (!$this->process($value)) {
$error = 'Didnt work out!';
$this->set(compact('error'));
} else {
$success = true; // Or a text like 'You did it!'
$this->set(compact('success'));
}
```

In the case of x-editable as "edit in place" JS all you need is to check for the error message:
```js
success: function(response, newValue) {
if (response.error) {
return response.error; //msg will be shown in editable form
}
}
```

## Configs

- 'autoDetect' => true // Detect AJAX automatically, regardless of the extension
Expand Down
9 changes: 7 additions & 2 deletions src/View/AjaxView.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(
* The rendered content will be part of the JSON response object and
* can be accessed via response.content in JavaScript.
*
* If an error has been set, the rendering will be skipped.
* If an error or success has been set, the rendering will be skipped.
*
* @param string|null $view The view being rendered.
* @param string|null $layout The layout being rendered.
Expand All @@ -98,13 +98,18 @@ public function __construct(
public function render($view = null, $layout = null) {
$dataToSerialize = [
'error' => null,
'success' => null,
'content' => null,
];

if (!empty($this->viewVars['error'])) {
if (isset($this->viewVars['error'])) {
$dataToSerialize['error'] = $this->viewVars['error'];
$view = false;
}
if (isset($this->viewVars['success'])) {
$dataToSerialize['success'] = $this->viewVars['success'];
$view = false;
}

if ($view !== false && !isset($this->viewVars['_redirect']) && $this->_getViewFileName($view)) {
$dataToSerialize['content'] = parent::render($view, $layout);
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Middleware/AjaxMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function testDefaultsRender() {

$expected = [
'error' => null,
'success' => null,
'content' => 'My Ajax Index Test ctp',
'_message' => $expected,
];
Expand Down Expand Up @@ -237,6 +238,7 @@ public function testActions() {

$expected = [
'error' => null,
'success' => null,
'content' => 'My Ajax Index Test ctp',
'_message' => null,
];
Expand Down Expand Up @@ -297,6 +299,7 @@ public function testSetVars() {

$expected = [
'error' => null,
'success' => null,
'content' => $this->Controller->viewVars['content'],
'_message' => null,
];
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/View/AjaxViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testSerialize() {

$response = $View->response;
$this->assertSame('application/json', $response->getType());
$expected = ['error' => null, 'content' => null, 'items' => $items];
$expected = ['error' => null, 'success' => null, 'content' => null, 'items' => $items];
$expected = json_encode($expected);
$this->assertTextEquals($expected, $result);
}
Expand All @@ -77,7 +77,7 @@ public function testRenderWithSerialize() {

$response = $View->response;
$this->assertSame('application/json', $response->getType());
$expected = ['error' => null, 'content' => 'My Ajax Index Test ctp', 'items' => $items];
$expected = ['error' => null, 'success' => null, 'content' => 'My Ajax Index Test ctp', 'items' => $items];
$expected = json_encode($expected);
$this->assertTextEquals($expected, $result);
}
Expand All @@ -104,7 +104,7 @@ public function testSerializeSetTrue() {

$response = $View->response;
$this->assertSame('application/json', $response->getType());
$expected = ['error' => null, 'content' => null, 'items' => $items, 'multiple' => $multiple];
$expected = ['error' => null, 'success' => null, 'content' => null, 'items' => $items, 'multiple' => $multiple];
$expected = json_encode($expected);
$this->assertTextEquals($expected, $result);
}
Expand All @@ -126,7 +126,7 @@ public function testError() {

$response = $View->response;
$this->assertSame('application/json', $response->getType());
$expected = ['error' => 'Some message', 'content' => null, 'items' => $items];
$expected = ['error' => 'Some message', 'success' => null, 'content' => null, 'items' => $items];
$expected = json_encode($expected);
$this->assertTextEquals($expected, $result);
}
Expand All @@ -147,7 +147,7 @@ public function testWithoutSubdir() {

$response = $View->response;
$this->assertSame('application/json', $response->getType());
$expected = ['error' => null, 'content' => 'My Index Test ctp'];
$expected = ['error' => null, 'success' => null, 'content' => 'My Index Test ctp'];
$expected = json_encode($expected);
$this->assertTextEquals($expected, $result);
}
Expand Down

0 comments on commit 030c75f

Please sign in to comment.