Issues

ZF-5987: Dijit parsing before store creation results in FilteringSelect and ComboBox dijits not working properly.

Description

If a ComboBox or FilteringSelect element is parsed before its store has been instantiated, it won't use the store. In previous versions of ZF, the ordering of dojo.addOnLoad functions put store creation before dijit parsing. In 1.7.6, dijits are parsed first (I believe this is because of [#ZF-4587]). This means forms using ComboBoxes and FilteringSelects which used to work in 1.7.5 no longer work correctly.

The following code, produced by 1.7.6 does not work:


dojo.require("custom.AutocompleteReadStore");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.Form");
dojo.require("dojo.parser");
dojo.addOnLoad(function() {
    dojo.forEach(zendDijits, function(info) {
        var n = dojo.byId(info.id);
        if (null != n) {
            dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
        }
    });
    dojo.parser.parse();
});
dojo.addOnLoad(function() {
    autocompleter = new custom.AutocompleteReadStore({"url":"http:\/\/foo.com\/userautocomplete","requestMethod":"get"});
});
var autocompleter;
var zendDijits = [{"id":"author_id","params":{"store":"autocompleter","dojoType":"dijit.form.FilteringSelect"}},{"id":"the_form","params":{"dojoType":"dijit.form.Form"}}];

The following, created by 1.7.5, however, does work:


dojo.require("custom.AutocompleteReadStore");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.Form");
dojo.require("dojo.parser");
dojo.addOnLoad(function() {
    autocompleter = new custom.AutocompleteReadStore({"url":"http:\/\/foo.com\/userautocomplete","requestMethod":"get"});
});
dojo.addOnLoad(function() {
    dojo.forEach(zendDijits, function(info) {
        var n = dojo.byId(info.id);
        if (null != n) {
            dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
        }
    });
    dojo.parser.parse();
});
var autocompleter;
var zendDijits = [{"id":"author_id","params":{"store":"autocompleter","dojoType":"dijit.form.FilteringSelect"}},{"id":"the_form","params":{"dojoType":"dijit.form.Form"}}];

Both examples result from the following addition to a Zend_Dojo_Form:


$author_fld = new Zend_Dojo_Form_Element_FilteringSelect( 
                'author_id',
                array(
                    'label'     => 'Author:',
                
                    // The javascript identifier for the data store:
                    'storeId'   => 'autocompleter',
                
                    // The class type for the data store:
                    'storeType' => 'custom.AutocompleteReadStore',
                
                    // Parameters to use when initializing the data store:
                    'storeParams' => array(
                        'url'           => 'http://foo.com/userautocomplete',
                        'requestMethod' => 'get',
                    ),
            ));
            
$form->addElement( $author_fld );

Comments

I am experiencing the same side-effects from the new changes in 1.7.6. Funny because I remember wanting the onLoadCaptureStart to function in this manner so we could utilize dojo.connect statements on parsed widgets - didn't foresee this problem though...

It appears as if the prependOnLoad() method of the Dojo view helper would solve this problem - simply create the data store like so:

$this->dojo()->prependOnLoad('function(){newMentorStore = new dojo.data.ItemFileReadStore();}');

For me, however, the dojo.onLoad() script that this generates is still placed after the ZF parsing function like so:

dojo.addOnLoad(function() { dojo.forEach(zendDijits, function(info) { var n = dojo.byId(info.id); if (null != n) { dojo.attr(n, dojo.mixin({ id: info.id }, info.params)); } }); dojo.parser.parse(); }); dojo.addOnLoad(function(){newMentorStore = new dojo.data.ItemFileReadStore();});

I was just able to find a work-around for this issue that might help in brainstorming for a solution...

Instead of assigning the store to the dojo widget using the Zend_Dojo_Form view helper method:

    $mentorSelectionForm->addElement('filteringSelect', 'child_primary_mentor', array(
                                     'label'        => 'Primary Mentor',
                                     'onChange'     => 'mentorSelectionChanged',
                                     'store'        => 'mentorStore',
                                     'searchAttr'   => 'mentor_name'
                                    ));

Create the element without assigning the store and in the dojo()->onLoadCaptureStart() (since we can now grab our widgets using dijit.byId) create the store as usual and then assign it to the widget like so:

    mentorStore = new dojo.data.ItemFileReadStore({
      url: '/blablabla',
      clearOnClose: true
    });
    dijit.byId('child_primary_mentor').attr('store', mentorStore);

Fixed in trunk and 1.7 release branch

This issue is not fixed. Can you please check it again?

I have got JavaScript error: 'dojo.data is undefined' after migrating to ZF 1.7.7. It seems that JavaScript tried to create new instance of dojo.data.ItemFileReadStore, but it is not loaded yet.

ItemFileReadStore instance should be created after page load, using dojo.addOnLoad method. I think that using prependOnLoad (look at the first comment) instead of addJavascript (in ComboBox.php line 142).

The following code was generated by Dojo View helper:


dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.form.FilteringSelect");
dojo.require("dijit.form.CurrencyTextBox");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dijit.form.Textarea");
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");
dojo.require("dijit.ColorPalette");
dojo.require("dojo.parser");

dojo.addOnLoad(function() {
    dojo.forEach(zendDijits, function(info) {
        var n = dojo.byId(info.id);
        if (null != n) {
            dojo.attr(n, dojo.mixin({ id: info.id }, info.params));
        }
    });
    dojo.parser.parse();
});

var manufacturerStore = new dojo.data.ItemFileReadStore({"url":"\/manufacturer\/list"});
var modelStore = new dojo.data.ItemFileReadStore({"url":"\/model\/list"});
var zendDijits = [{"id":"manufacturer","params":{"label":"Manufacturer","store":"manufacturerStore","dijitParams":{"searchAttr":"name","ignoreCase":"true","autocomplete":"true"},"dojoType":"dijit.form.FilteringSelect"}},{"id":"model","params":{"label":"Models","store":"modelStore","dijitParams":{"searchAttr":"name","ignoreCase":"true","autocomplete":"true"},"dojoType":"dijit.form.FilteringSelect"}},{"id":"price","params":{"currency":"USD","required":"true","dojoType":"dijit.form.CurrencyTextBox"}},{"id":"year","params":{"places":0,"constraints":"{\"min\":1970,\"max\":2009,\"pattern\":\"####\"}","required":"true","dojoType":"dijit.form.NumberTextBox"}},{"id":"mileage","params":{"places":4,"dojoType":"dijit.form.NumberTextBox"}},{"id":"description","params":{"required":"true","style":"width: 200px;","dojoType":"dijit.form.Textarea"}},{"id":"save","params":{"label":"\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c","dojoType":"dijit.form.Button"}}];

Hello,

I'm having problems with this issue that still exists in ZF 1.8.3, is there any chance of having it really fixed soon?

Please see ZF-6148 -- a solution is now in trunk and the 1.8 release branch, and will release with 1.8.4.

Hi again,

I'm having save problems with ZF 1.8.4. Looks like the code is not yet in 1.8.4

Please update,

Thanks,