2009年10月2日星期五

[fw-mvc] Conflict between ContextSwitch and AjaxContext ?

Hi there,

I'm writing an article about ContextSwitch and AjaxContext and, for demonstration purposes, I'm working on a really light example wich illustrate both ContextSwitch and AjaxContext.

I don't know exactly why, but I can't use both a "classical" and an Ajax context on a same action.

Example :
I want to display some data from an ini file, the default context is in HTML with a layout.
I have an xml view for my data, and an Ajax search, using AjaxContext.

Xml context is working fine : If I give a format parameter with XML as a value, my Xml view is displayed. Great.
But Ajax context doesn't work : If I don't give a format parameter, it display the entire page with the layout. Logical behavior. If I give an "html" format, ContextSwitch throws an exception "html context does not exists"... It exists, but not for ContextSwitcher.

I manage to make it works, by forcing an html context for AjaxContext, without giving a format parameter with my Ajax call.

Here is my controller code :

    public function init()
    {           
        $this->_helper->ajaxContext
            ->addActionContext('index', 'html');
           
        $this->_helper->contextSwitch
            ->addActionContext('index', 'xml')
            ->initContext();
    }

    public function indexAction()
    {
    $this->_helper->ajaxContext->initContext('html');

        $dataIni = new Zend_Config_Ini(APPLICATION_PATH . '/../data/data.ini');
        $data = $dataIni->toArray();
        $data = $data['sites'];
        $search = $this->_getParam('search');
       
        if (! empty($search)) {
            $filteredData = array();
           
            foreach ($data as $row) {
                if (stripos($row['name'], $search) === 0) {
                    $filteredData[] = $row;
                }
            }
           
            $data = $filteredData;
        }
      
        $this->view->data = $data;
    }

I can deal with this restriction (or issue ?) but I must explain it, as I'm writing an article. Why ContextSwitcher throws an exception when an html AjaxContext format is given on the same action ?

Thanks for Help,
Lucas

没有评论: