PDA

View Full Version : Store findById uses old dataset after second load()



schtono
24 Jun 2011, 12:25 PM
TLDR: findById() does not work after different dataset is loaded into store.

Given the following situation:

(1) Create a datastore and set an arbitrary filter upon instatiation
(2) load() a dataset through a json-proxy
(3) retrieve arbitrary record via store.findById() -> returns the correct record
(4) change the url-parameters of the corresponding proxy to fetch a slightly different dataset
(5) load() the "different" dataset
(6) store.findById() does not return the correct record in the new dataset. Instead, it returns the corresponding record from the previous dataset.

I investigated the issue and found that store.findById() works on either the actual dataset or a snapshot (this.data vs. this.snapshot in the source). The second load() command (see 5) probably does not correctly reset the snapshot of data.

Best
Philipp

elmasse
25 Jun 2011, 7:10 AM
I have the same issue, I've just override the getById method on Store instance to not use snapshot and that fixed the problem, at least as a quick workaround.

I think it has to do with snapshot, when filter method is called on the second load (no matter if you have added a filter or not) this.snapshot is a valid object but whit wrong data and this line makes the difference:



this.snapshot = this.snapshot || this.data.clone();


So, as I said, my quick fix is to override the getById method from:


getById : function(id) {
return (this.snapshot || this.data).findBy(function(record) {
return record.getId() === id;
});
},


to:



getById : function(id) {
return this.data.findBy(function(record) {
return record.getId() === id;
});
}

schtono
25 Jun 2011, 8:18 AM
Thanks for posting the fix, I use the same solution and it works. Dirty though ;)

Travisrowland
28 Jan 2012, 4:22 AM
Oh my! I am so glad I found this, it was driving me up the freaking wall. Thanks for the fix. :)

findajit
28 Jan 2012, 10:12 PM
TLDR: findById() does not work after different dataset is loaded into store.

Given the following situation:

(1) Create a datastore and set an arbitrary filter upon instatiation
(2) load() a dataset through a json-proxy
(3) retrieve arbitrary record via store.findById() -> returns the correct record
(4) change the url-parameters of the corresponding proxy to fetch a slightly different dataset
(5) load() the "different" dataset
(6) store.findById() does not return the correct record in the new dataset. Instead, it returns the corresponding record from the previous dataset.

I investigated the issue and found that store.findById() works on either the actual dataset or a snapshot (this.data vs. this.snapshot in the source). The second load() command (see 5) probably does not correctly reset the snapshot of data.

Best
Philipp


Please paste an example code to reproduce this problem!

Travisrowland
29 Jan 2012, 3:26 AM
I posted a way to reproduce the issue here: http://www.sencha.com/forum/showthread.php?167673-Load-new-Data-in-Store-but-LoadRecord-loads-old-Data-in-formPanel

izak18
26 Apr 2012, 12:05 PM
Hi everybody, I have the same problem when I use


var listaDetDoc = new Ext.List({
id: 'listaDetDoc',
store: detalleMaterialStore,
itemTpl:
'<div>
'<span class="list-izq">{Posicion} - {CodMaterialCorto}</span>'+
'</div>',
listeners: {
'render': function (thisComponent) {
thisComponent.getStore().load();
if(detalleMaterialStore.last()){
var doc = listaDetDocPanel.getRecord().data.id;
thisComponent.getStore().filter('idDoc',doc);
}
}
}
});

How I can use your fix?
Thanks in advance!

Travisrowland
27 Apr 2012, 12:58 AM
To use the fix, you just put that getById function in your store, and it will override the default one.

izak18
27 Apr 2012, 12:51 PM
Hi Travisrowland, I did what you said, but doesn't work, I think is because the original method of this post is getById() but I'm using filter(), in my case I think will be like this:


var repartoStore = new Ext.data.Store ({
autoLoad: true,
model: 'repartoModel',
filter : function(id) {
return this.data.findBy(function(record) {
return record.getId() === id;
});
}
});

I'm not pretty sure what I have to do. Still not working.
Thanks for your time.