only provide one alternate context format. The switch to this context is triggered via the detection of the isXmlHttpRequest() member of the request object. However, currently one also has to supply a format parameter with the request. This seems redundant.
I copy a thread from the MVC list below. This shows that it is fairly trivial to extend AjaxContext to automatically check is there is just one context and if so use it.
My thought though is that it may be nice to work this feature into the base ContextSwitch class. The idea would be that the first context format given would be the default, which would be applied if no format string was given, whereas the later formats would require you to provide the format parameter.
On 16 Apr 2008, at 21:25, Taylor Barstow wrote:
On Apr 11, 2008, at 10:10 AM, Taylor Barstow wrote:
When using the AjaxContext helper, it seems odd to me that I have to add ?format=X to my URIs in order to trigger the context switch, especially if I have only specified one possible format.
Shouldn't the X-Requested-With: XMLHttpRequest header be enough? Is there any way that I can trigger the context switch without passing the format parameter when there is only one possible format?
To follow up on this, I looked further into it and you can actually make this happen yourself:
class MyController extends Zend_Controller_Action {
public $ajaxable = array('index' => array('html'));
public function init()
Unknown macro: {
$this->_helper->ajaxContext->initContext('html');
}
}
BIG CAVEAT: You would only want to do this if you only have one context format for the entire controller.
I hope this helps someone else
Taylor
Following on from this suggestion, I think the following should allow us to forgo specifying the format parameter whilst still allowing multiple contexts within a single controller.
(I wasn't able to find a feature request for this sort of change to AjaxContext. Do people think it is worth filing one?)
Regards,
Carlton
<?php
/**
- Removes need to specify format param when providing unique ajax context.
*/
class My_Controller_Action_Helper_UniqueAjaxContext extends Zend_Controller_Action_Helper_AjaxContext
{
/**
- Automatically initialise unique AjaxContext on a per action basis.
*/
public function initContext($format = null)
{
$action = $request->getActionName();
$context = $this->getActionContexts($action)
//check in case multiple contexts DO exist
if(count($context) === 1)
Unknown macro: {
$format = $context[0];
}
return parent::initContext($format);
}