Search code examples
extjsstore

ExtJS 6. Use data from 1st store in 2nd store constructor


There are 2 stores. I need data from 1st store in 2nd store constructor. How can it be done?


Solution

  • This would be rather easy, but useless.

    To achieve what you want you have to create store2 from the load() event of store1, and you may not bind the store to any component before said load event.

    Ext.define('MyApp.store.Store1',{
        ...
        listeners:{
            load:function(store, records) {
                var store2 = Ext.getStore("store2") || Ext.create('MyApp.store.Store2',{
                    store1data: records
                });
                Ext.getCmp("someCombobox").bindStore(store2); // you would have to use ext-empty-store on the combobox before, or else it may trigger store creation too early
            }
    

    BUT this is not good programming style nor does it serve any real purpose. Normally you would want to call some function for store2 again whenever store1 gets new data. There is nearly nothing you have to do during store construction, that cannot be done afterwards using reconfigure or other store methods. (notable exception: you have to decide during construction whether it is an array store or a json store or a tree store).

    So for instance you can define a method on store2 and call that just after store1 has been reloaded:

    store2.someFunction = function(store1Records) {
        var store2 = this;
        Ext.each(store1Records,function(record) {
            store2.getProxy().setExtraParam(record.get("paramName"),record.get("paramValue"));
        });
        store2.load();
    }
    
    store1.on('load',function(store, records) {
        store2.someFunction(records);
    });