PDA

View Full Version : Problem with store .findBy



louis.maconi
16 Jan 2015, 8:12 AM
So, I'm using .findBy in multiple places in virtually the same way, and all work just fine, except one, and I can't figure out why only this one doesn't work. Any suggestions or ideas appreciated.

Here's the problem child:

Model:


Ext.define('SW101.model.tisRasiModel', {
extend: 'Ext.data.Model',

requires: [
'Ext.data.Field'
],

fields: [
{
name: 'id'
},
{
name: 'modified'
},
{
name: 'modifiedBy'
},
{
mapping: 'jobFunction.id',
name: 'jobFunction'
},
{
mapping: 'tisSheet.id',
name: 'tisSheet'
},
{
mapping: 'rasi.name',
name: 'rasi'
}
]
});

Store:


Ext.define('SW101.store.tisRasiStore', {
extend: 'Ext.data.Store',

requires: [
'SW101.model.tisRasiModel',
'Ext.data.proxy.Rest',
'Ext.data.reader.Json'
],

constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: false,
autoSync: true,
model: 'SW101.model.tisRasiModel',
storeId: 'tisRasiStore',
proxy: {
type: 'rest',
url: 'api/tis_rasi',
headers: {
Accept: 'application/json'
},
reader: {
type: 'json'
}
},
listeners: {
write: {
fn: me.onJsonstoreWrite,
scope: me
}
}
}, cfg)]);
},

onJsonstoreWrite: function(store, operation, eOpts) {
store.reload();
}

});


And the place where I'm using the .findBy function (this is in a renderer function, so 'store' references a different, passed-in store). In this example, the index returned is always -1, even though I know there should be many instances of actual returned values:


//only enable delete icon if the job function has no tis/rasis under it.
var deleteIcon = this.items[1];
var trStore = Ext.getStore('tisRasiStore');
trStore.load();
//console.log(trStore);

var hasFilter = trStore.isFiltered();
if(hasFilter){
trStore.clearFilter();
store.load();
}
var jfID = record.data.id;
var trIndex = trStore.findBy(
function(trRecord, trID){
//console.log(trRecord.get('jobFunction') + '|' + jfID);
if(trRecord.get('jobFunction') == jfID){
return true;
}
return false;
}
);
console.log(trIndex);


//enable ONLY if tis/rasi does not exist (that's what -1 means)
if(trIndex == -1){
deleteIcon.disabled = false;
} else {
deleteIcon.disabled = true;
}

chamacs
16 Jan 2015, 8:40 AM
The problem is the trStore.load() is an async call. All code after it is running right after the call. It does not wait for the store to be loaded. So the findBy call is searching an empty store. You must sequentially handle this via the load() callback. Something like:


trStore.load({
callback : function(records, operation, success) {
trStore.findBy( ..... );
},
scope : this
});


Hope this helps.

louis.maconi
16 Jan 2015, 9:57 AM
Thanks! That pointed me in the right direction. :)