views:

896

answers:

1

hi,

I am having a few problems with Dojo Filtering Selects when using the Zend Framework Forms and need some help to find out what I have missed as this is driving me mad.

I am currently getting this errors in firebug:

  • dojo.data is undefined
  • dojo.data.ItemFileReadStore is not a constructor

Below is the code that I am using to create the filter select and provide the json data to the calling controller.

Zend_Form Element (Dojo Enabled)

$industry = new Zend_Dojo_Form_Element_FilteringSelect('industry');
    $industry->setAutocomplete(true)
    ->setStoreId('industrystore')
    ->setStoreType('dojo.data.ItemFileReadStore')
    ->setStoreParams(array('url' => $baseUrl.'/dojo/industry'))
    ->setAttrib("searchAttr", "title")
    ->setRequired(true)
    ->removeDecorator('DtDdWrapper')
    ->removeDecorator('label')
    ->removeDecorator('HtmlTag');

Dojo Controller

public function industryAction(){

    $db = Zend_Db::factory($this->config->database);

    $result = $db->fetchAll("SELECT * FROM industries");
    $data = new Zend_Dojo_Data('industryid', $result);
    $this->_helper->autoCompleteDojo($data);

    $db->closeConnection();
}

The annoying thing is all my other Dojo elements on this form and other forms work well it is just whenever I do Filtering Selects that I hit these problems, and this problem causes all the other elements in a form to fail too.

Thanks in advance.

A: 

The problem is actually with how Zend Framework initializes the dijits and data stores before the toolkit is fully loaded, in this case specifically the methods assigning the store to the dijit. I ran into this issue as well and found the best way to work around the issues was to either pass the data store from the controller to a JavaScript variable defined in the view or do what your did with a specific autocomplete action. Based on your example I would make the following changes.

In your form I would simplify the element:

$industry = new Zend_Dojo_Form_Element_FilteringSelect('industry');
    $industry->setAutocomplete(true)
    ->setRequired(true)
    ->removeDecorator('DtDdWrapper')
    ->removeDecorator('label')
    ->removeDecorator('HtmlTag');

In your view you want to connect the store to your dijit and make sure that you have loaded the dojo.data.ItemFileReadStore module:

<?php $this->dojo()->onLoadCaptureStart()?>
    function(){
        dijit.byId('industry').store = new dojo.data.ItemFileReadStore({ url: '/controller/industry' });
    }
<?php 
    $this->dojo()->onLoadCaptureEnd();
    $this->dojo()->requireModule('dojo.data.ItemFileReadStore');
?>

As I mentioned I ran into a similar issue which I answered here . Another issue I discovered is that the data store does not like dealing with labels declared anything other than "name" for the label declaration in the Zend_Dojo_Data.

Erik