Skip to content

Commit 3accba4

Browse files
authored
Merge pull request #35 from dereuromark/feature/helpers
Make AjaxView extend AppView
2 parents 43d67dc + 4853867 commit 3accba4

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

docs/View/Ajax.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ public function statesAjax() {
6262
```
6363

6464
## Custom Plugin helpers
65-
If your view classes need plugin helpers, and you are not using the controller way anymore to load/define helpers, then you might need to extend the view class to project level and add them there:
65+
If your view classes needs additional plugin helpers, and you are not using the controller way anymore to load/define helpers, then you might need to extend the view class to project level and add them there:
6666
```php
6767
namespace App\View;
6868

69-
use Cake\View\View;
7069
use Ajax\View\AjaxView as PluginAjaxView;
7170

7271
class AjaxView extends PluginAjaxView {
@@ -75,14 +74,15 @@ class AjaxView extends PluginAjaxView {
7574
* @return void
7675
*/
7776
public function initialize() {
77+
parent::initialize();
7878
$this->loadHelper('...);
7979
...
8080
}
8181

8282
}
8383
```
8484
Then make sure you load the app `Ajax` view class instead of the `Ajax.Ajax` one.
85-
If you are using the component, you can set Configure key `'Ajax.viewClass'` to your `App\View\AjaxView` here.
85+
If you are using the component, you can set Configure key `'Ajax.viewClass'` to your `'Ajax'` here.
8686

8787
## Tips
8888
I found the following quite useful for your jQuery AJAX code as some browsers might not properly work without it (at least for me it used to).

src/View/AjaxView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
namespace Ajax\View;
33

4+
use App\View\AppView;
45
use Cake\Event\EventManager;
56
use Cake\Http\Response;
67
use Cake\Http\ServerRequest;
78
use Cake\Utility\Hash;
8-
use Cake\View\View;
99

1010
/**
1111
* A view to handle AJAX requests.
@@ -20,7 +20,7 @@
2020
* @author Mark Scherer
2121
* @license http://opensource.org/licenses/mit-license.php MIT
2222
*/
23-
class AjaxView extends View {
23+
class AjaxView extends AppView {
2424

2525
const JSON_OPTIONS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PARTIAL_OUTPUT_ON_ERROR;
2626

tests/TestApp/src/View/AppView.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace TestApp\View;
4+
5+
use Cake\View\View;
6+
7+
class AppView extends View {
8+
}

tests/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464

6565
Cake\Core\Plugin::load('Ajax', ['path' => ROOT . DS, 'bootstrap' => true]);
6666

67+
class_alias(\TestApp\View\AppView::class, 'App\View\AppView');
68+
6769
// Ensure default test connection is defined
6870
if (!getenv('db_class')) {
6971
putenv('db_class=Cake\Database\Driver\Sqlite');

0 commit comments

Comments
 (0)