|
|
|
tonystamp
|
I have been having some trouble obtaining the html view rendered by an action from an AJAX request. Initially, in my ajax-ed controller action, i was checking if the request was XmlHttpRequest, and if so, disabling the layout so just the portion of the view is returned. This was not working, so a little research found pointers towards the AjaxContext helper. I have set it up as follows:
Controller: public function init(){ parent::init(); $this->flashMessenger = $this->getHelper('FlashMessenger'); $ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('view', 'html') ->initContext(); } public function viewAction(){ if(!($id = $this->getRequest()->getParam('newsID'))){ $this->_redirect('index'); } $newsTable = new News_Table(); $select = $newsTable->select(); $select->setIntegrityCheck(false); $select->from('news')->joinLeft('news_approved', 'news.id = news_approved.news_id', array('approver_id' => 'staff_id', 'date_approved')); $select->where('id = ?',(int) $id); $rowset = $newsTable->fetchAll($select); $this->view->assign('news', $rowset->current()); } The view script rendered for this is called view.phtml, so as per the documentation, i copied the content and named a new view script view.ajax.phtml. Although the correct action is being called (from jquery, with format=html appended to the query string, although it would be nice to assume that no format parameter = html by default, unless this can be set somewhere?) and loading the correct ajax view script, it is not disabling the layout, and i am getting a "page within a page". Any pointers on what i'm doing wrong? |
||||||||||||||||
|
Daniel Latter
|
You need to call setNoRender(true) of the viewRenderer helper inside
your action. On 1 Nov 2009, at 13:13, tony stamp <[hidden email]> wrote: > > I have been having some trouble obtaining the html view rendered by > an action > from an AJAX request. Initially, in my ajax-ed controller action, i > was > checking if the request was XmlHttpRequest, and if so, disabling the > layout > so just the portion of the view is returned. This was not working, > so a > little research found pointers towards the AjaxContext helper. I > have set it > up as follows: > > Controller: > public function init(){ > parent::init(); > $this->flashMessenger = $this->getHelper('FlashMessenger'); > > $ajaxContext = $this->_helper->getHelper('AjaxContext'); > $ajaxContext->addActionContext('view', 'html') > ->initContext(); > } > > public function viewAction(){ > if(!($id = $this->getRequest()->getParam('newsID'))){ > $this->_redirect('index'); > } > > $newsTable = new News_Table(); > $select = $newsTable->select(); > $select->setIntegrityCheck(false); > $select->from('news')->joinLeft('news_approved', > 'news.id = > news_approved.news_id', > array('approver_id' => > 'staff_id', 'date_approved')); > $select->where('id = ?',(int) $id); > > $rowset = $newsTable->fetchAll($select); > $this->view->assign('news', $rowset->current()); > } > > The view script rendered for this is called view.phtml, so as per the > documentation, i copied the content and named a new view script > view.ajax.phtml. > > Although the correct action is being called (from jquery, with > format=html > appended to the query string, although it would be nice to assume > that no > format parameter = html by default, unless this can be set > somewhere?) and > loading the correct ajax view script, it is not disabling the > layout, and i > am getting a "page within a page". > > Any pointers on what i'm doing wrong? > -- > View this message in context: http://old.nabble.com/Returning-HTML-content-from-AjaxContext-tp26148891p26148891.html > Sent from the Zend Framework mailing list archive at Nabble.com. > |
||||||||||||||||
|
tonystamp
|
I'm not sure that's right - i thought the AjaxContext would handle that, and besides, that means i'd have to place conditionals inside each action to see if the request came from an XHR - if so, disable rendering, if not, carry on as normal. Isn't AjaxContext supposed to be transparent?
|
||||
|
scss
|
Yes it is supposed to as far as I know.
I had similar problems with AjaxContent. And I used $contextSwitch = $this->_helper->getHelper('contextSwitch'); instead. I call all my ajaxified actions with /format/xml or format/json or format/html This solved my problems. On Mon, Nov 2, 2009 at 12:47 PM, tony stamp <[hidden email]> wrote: > > I'm not sure that's right - i thought the AjaxContext would handle that, and > besides, that means i'd have to place conditionals inside each action to see > if the request came from an XHR - if so, disable rendering, if not, carry on > as normal. Isn't AjaxContext supposed to be transparent? > > > > Daniel Latter-2 wrote: >> >> You need to call setNoRender(true) of the viewRenderer helper inside >> your action. >> >> >> On 1 Nov 2009, at 13:13, tony stamp <[hidden email]> wrote: >> >>> >>> I have been having some trouble obtaining the html view rendered by >>> an action >>> from an AJAX request. Initially, in my ajax-ed controller action, i >>> was >>> checking if the request was XmlHttpRequest, and if so, disabling the >>> layout >>> so just the portion of the view is returned. This was not working, >>> so a >>> little research found pointers towards the AjaxContext helper. I >>> have set it >>> up as follows: >>> >>> Controller: >>> public function init(){ >>> parent::init(); >>> $this->flashMessenger = $this->getHelper('FlashMessenger'); >>> >>> $ajaxContext = $this->_helper->getHelper('AjaxContext'); >>> $ajaxContext->addActionContext('view', 'html') >>> ->initContext(); >>> } >>> >>> public function viewAction(){ >>> if(!($id = $this->getRequest()->getParam('newsID'))){ >>> $this->_redirect('index'); >>> } >>> >>> $newsTable = new News_Table(); >>> $select = $newsTable->select(); >>> $select->setIntegrityCheck(false); >>> $select->from('news')->joinLeft('news_approved', >>> 'news.id = >>> news_approved.news_id', >>> array('approver_id' => >>> 'staff_id', 'date_approved')); >>> $select->where('id = ?',(int) $id); >>> >>> $rowset = $newsTable->fetchAll($select); >>> $this->view->assign('news', $rowset->current()); >>> } >>> >>> The view script rendered for this is called view.phtml, so as per the >>> documentation, i copied the content and named a new view script >>> view.ajax.phtml. >>> >>> Although the correct action is being called (from jquery, with >>> format=html >>> appended to the query string, although it would be nice to assume >>> that no >>> format parameter = html by default, unless this can be set >>> somewhere?) and >>> loading the correct ajax view script, it is not disabling the >>> layout, and i >>> am getting a "page within a page". >>> >>> Any pointers on what i'm doing wrong? >>> -- >>> View this message in context: >>> http://old.nabble.com/Returning-HTML-content-from-AjaxContext-tp26148891p26148891.html >>> Sent from the Zend Framework mailing list archive at Nabble.com. >>> >> >> > > -- > View this message in context: http://old.nabble.com/Returning-HTML-content-from-AjaxContext-tp26148891p26156668.html > Sent from the Zend Framework mailing list archive at Nabble.com. > > |
||||||||||||||||
|
asagala
|
In reply to this post
by tonystamp
Maybe this is totally off topic but I know when I want to return JSON data from one of my controller actions I use the json helper. You use it like this $this->_helper->json($data); where $data is an array of data that you want to send by JSON. NIce thing is that is also disables the view. No need to call anything else.
|
||||||||||||||||
|
tonystamp
|
Thanks for the reply. It returns json fine, but i am looking at just the view script being returned, not the layout. It seems like the AjaxContext is not disabling the layout?
|
||||||||||||||||
|
tonystamp
|
In reply to this post
by tonystamp
ok, i think i've solved the problem. It was quite obscure, so if anyone has similar problems i'll explain it here.
I have the following front controller plugin that loads the correct layout depending on the request compared to a value in the layout.ini: Layout.ini [layout] layoutPath = "../application/views/layouts/" contentKey = "content" viewSuffix = "phtml" layout = "site" [site : layout] layout = "site" [admin: layout] layout = "admin" controllers = "News, Staff, Products" ...and a front controller plugin that reads this and loads the correct layout file depending on the request: LayoutPlugin: <?php /** * @package Controller * @subpackage Plugin * @uses Zend_Config * @see Layout.ini * * Layout plugin selects the layout to be set for the current action defined in the layout.ini * Especially useful for admin or auth'ed areas, instead of eg: * <code> * //auth'ed controller * public function postDispatch(){ * $this->getHelper('layout')->setLayout('adminLayout'); * } * </code> * ... the controllers are assigned in the layout.ini, under the key 'controllers': * * The plugin loads the Layout object with the configurations defined in the section that a controller -> request match is made * */ class My_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract{ /** * @var Zend_Config */ protected $config; /** * @param Zend_Config $config */ public function __construct(Zend_Config $config){ $this->config = $config; } public function preDispatch(Zend_Controller_Request_Abstract $request) { $this->isControllerDefinedInConfig($request->getControllerName()); } /** * Checks whether the action belongs to a controller defined with a specific layout * * @param string $controllerName From request * @return void */ protected function isControllerDefinedInConfig($controllerName){ foreach($this->config as $configSection){ foreach($configSection as $key => $val){ if(strtolower($key) == 'controllers'){ $controllers = explode(',', $configSection->$key); foreach($controllers as $controller){ if(strtolower(trim($controller)) == strtolower(trim(str_ireplace(array('-'), '', $controllerName)))){ $layout = Zend_Layout::getMvcInstance(); $layout->setOptions($configSection); } } } } } } } ?> A careful look of the above and you'll see that this is executed on the controllers PostDispatch method - i changed it to PreDispatch, and everything is working as it should, with just the .ajax.phtml view script being rendered without the layout. It seems like it was resetting the layout or something since the layout was being modified after the action? Unsure...but fixed! As a pointer - if the layout is still being rendered from AjaxContext, go through your code to see if you're modifying the layout anywhere.
|
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |