4

I have a proxy store that fills through two different ways - either via loadData, or via load.

I have certain actions that should take place once the store is filled; namely, a certain record should be searched and selected:

preselectByName:function(name, groupName) {
    var store = this;
    if(store.preselected) return;
    store.preselected = true;
    store.on('load',function() {
        store.selectByName(name, groupName);
        store.preselected = false;
    },store,{single:true});
}

called like this:

if(store.isLoaded) store.selectByName(name, groupName);
else store.preselectByName(name, groupName);

This code works fine if the store fills via load, but not via loadData.

  • Is there another event that is fired from both load and loadRecord?
  • How else would I implement a listener that fires on any of the two events, whichever comes first?
Alexander
  • 19,906
  • 19
  • 75
  • 162

2 Answers2

3

is an event, that fires from both load and loadRecord, but be careful, it fires from any change that made to the dataset.

Besides: I usually use this to find the event that I need in ExtJs:

Ext.util.Observable.capture(myObj, function(evname) {console.log(evname, arguments);})

It captures all ExtJs events, fired by the myObj component and logs it to the console.

  • Or you could create a custom event, which you fire manually on load and after loadData, like:

store.on("load",function(){store.fireEvent("myload")})

and

store.loadData(...)

store.fireEvent("myload")

then

store.on("myload",function(){...})

  • Yes, it may be a problem that `datachanged` fires too often. I will check it out; but since the records in the store can be edited in forms, and I only want to fire on `load` or `loadData`, I am not sure this is the way to go. – Alexander Apr 21 '17 at 14:53
  • Maybe you could try something: instead of `loadData()`, you could try to change the proxy to a `memory proxy` ,set the `data` property to the required data, then `load()`, and finally in the `load event` change back the proxy to the original . I have not tested this, just an idea. Maybe a bit clanky. – Zsolt Medgyesi Apr 21 '17 at 16:37
2

The load event is specifically for reading data from a remote data source. The store can be extended to introduce an event that satisfies your requirements.

/*
    @event loadrecords Fires whenever records a loaded into the store.
        @param {Ext.data.Model[]} records An array of records
*/
Ext.define('MyApp.data.Store', {
    extend: 'Ext.data.Store',

    load: function() {
        this.callParent(arguments);
        this.fireLoadRecords();
        return this;
    },

    fireLoadRecords: function() {
        this.fireEvent('loadrecords', this.getRange());
    },

    loadRecords: function(records, options) {
        this.callParent(arguments);
        this.fireLoadRecords();
    }
});

http://docs.sencha.com/extjs/6.2.0/classic/Ext.data.ProxyStore.html#event-load

Trevor Karjanis
  • 1,485
  • 14
  • 25