Andy Baird
Andy Baird

Reputation: 6218

Using _forward with AjaxContext

I have a controller setup that uses a whole bunch of different AjaxContent helpers. My init() for the controller looks something like this:

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('index', 'html')
        ->addActionContext('compose', 'html')
        ->addActionContext('sent', 'html')
        ->addActionContext('recipients','html')
        ->addActionContext('inbox', 'html')
        ->addActionContext('sendsuccess','html')
        ->initContext();

At the end of the composeAction(), if a certain condition is met, the AJAX request should forward to sendsuccessAction().

Doing this with the standard _forward() method doesn't seem to forward it as an AjaxContent request - the page wants to render using the standard view template.

Any ideas on how I can use _forward or some other redirect method but keep the request as an AJAX request so the proper action context fires?

Upvotes: 2

Views: 1122

Answers (2)

jason
jason

Reputation: 8918

Basically what's happening is that the initContext() method (which sets up the environment for an ajax response) only gets called for your first dispatched action, not the second.

There's a bunch of different ways around this.

First, to verify this is the issue, try calling

$this->_helper->ajaxContext->initContext();

from your sendsuccessAction. This will force the AjaxContext action helper to properly set up the viewRenderer again.

Alternatively, you could move the call to $ajaxContext->initContext() from the init() method to the preDispatch() method of your controller. This will cause it to run before each action is dispatched.

Upvotes: 2

Rufinus
Rufinus

Reputation: 30741

Use:

$this->_forward("action","controller","module",array('format'=>'html')); # or ajax or json or whatever

Upvotes: 0

Related Questions