Skip to content

Commit

Permalink
Merge pull request #35 from dereuromark/feature/helpers
Browse files Browse the repository at this point in the history
Make AjaxView extend AppView
  • Loading branch information
dereuromark authored Oct 31, 2019
2 parents 43d67dc + 4853867 commit 3accba4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/View/Ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ public function statesAjax() {
```

## Custom Plugin helpers
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:
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:
```php
namespace App\View;

use Cake\View\View;
use Ajax\View\AjaxView as PluginAjaxView;

class AjaxView extends PluginAjaxView {
Expand All @@ -75,14 +74,15 @@ class AjaxView extends PluginAjaxView {
* @return void
*/
public function initialize() {
parent::initialize();
$this->loadHelper('...);
...
}

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

## Tips
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).
Expand Down
4 changes: 2 additions & 2 deletions src/View/AjaxView.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Ajax\View;

use App\View\AppView;
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Utility\Hash;
use Cake\View\View;

/**
* A view to handle AJAX requests.
Expand All @@ -20,7 +20,7 @@
* @author Mark Scherer
* @license http://opensource.org/licenses/mit-license.php MIT
*/
class AjaxView extends View {
class AjaxView extends AppView {

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

Expand Down
8 changes: 8 additions & 0 deletions tests/TestApp/src/View/AppView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TestApp\View;

use Cake\View\View;

class AppView extends View {
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@

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

class_alias(\TestApp\View\AppView::class, 'App\View\AppView');

// Ensure default test connection is defined
if (!getenv('db_class')) {
putenv('db_class=Cake\Database\Driver\Sqlite');
Expand Down

0 comments on commit 3accba4

Please sign in to comment.