View Full Version : [SOLVED] problem with Data Store and getById() function...
violinista
29 May 2007, 3:52 AM
Helo to all guys,
Every day I learn something new and something special about extJs, but today I am in some "endless loop".
This is my case: I want to filter the data store with the value from textBox.
Data store is loaded from backend, and when I switch to Firebug, it is already loaded, with all array items (it is checked in console, when I type "console.log(ds)", and explore ds object).
My idea was to handle onKeyUp event from textbox (the code is shortened):
var txtCode=new Ext.form.NumberField({....});
Ext.get(txtCode.el).on('keyup', function(e){ ...
... and to filter datastore recordset with value entered into textbox. But, I cannot access records in any way:
- I have tried with ds.getById() - it returns "undefined". I pass correct ID to ds.GetById and yes, ds is loaded (checked in debug and console mode).
Data is readed via JSON reader, but I think it is not important.
If you see at the creenshot of console, you will notice that keys have values 100,101,102 etc. and if I try ds.getById(100) it returns "undefined".
Really nasty for me, plz help! thanks!
UPDATE:SOLVED! The reason for this not working is because ds.load() is asynchronous. Here is solution:
var ds = new Ext.data.Store({ .... });
ds.on("load", function(){
//do whatever with dataset, get records, etc.
});
ds.load();
Animal
29 May 2007, 4:17 AM
What does the Json look like?
If it's returning a string id, then you'll have to use a string to look up the Records.
violinista
29 May 2007, 4:23 AM
Thanks for your quick answer, it returns (in short):
{"rowCount":"20","rows":[{"Code":"100","Description":"Sample1"},{"Code":"110","Description"
:"Sample2"}]}
Here is reader setup:
var ds = new Ext.data.Store({
proxy: new Ext.data.ScriptTagProxy({
url:"http://localhost/makeJson.php"
}),
reader: new Ext.data.JsonReader({
totalProperty:"rowCount",
root:"rows",
id:"Code"
}, [
{name:"Code", type:"int"},
{name:'Description', type:"string"}
])
});
ds.load({
params:{
start:0,
limit:2
}
});
But yes, I've tried: ds.getById("100") - again, without result.
This JSON is working, because I am using reader which I've used earlier in some examples of dataGrid, and it worked perfect.
Any ideas?? Thank you again.
Animal
29 May 2007, 4:25 AM
What does ds.getCount() return?
And if not zero, what does ds.getAt(0) return?
violinista
29 May 2007, 4:33 AM
console.log(ds.getCount()); //returns 0
console.log(ds.getAt(0)); //returns "undefined"
Very strange! :-?
Animal
29 May 2007, 4:41 AM
So the version of "ds" that your are accessing at that point is not loaded.
violinista
29 May 2007, 4:47 AM
Yes, it is loaded: I am doing "console.log..." after ds.load, and when try:
console.log(ds);
...the result is in screenshot:
:s
Thank you very much for your patience, I know I am somewhat boring.
Animal
29 May 2007, 4:59 AM
There must be something else going on here.
ds.getCount() displays the number of items in the MixedCollection object that holds the Records.
What is you just type "ds.data", and press return, and explore that?
violinista
29 May 2007, 5:07 AM
it returns:
[ ]. - exactly
Animal
29 May 2007, 5:13 AM
What if you type
"ds instanceof Ext.data.Store"
and
"ds.data instanceof Ext.util.MixedCollection"
violinista
29 May 2007, 5:15 AM
It returns:
true
true
??
Animal
29 May 2007, 5:46 AM
so, just type "ds" then press return.
violinista
29 May 2007, 5:54 AM
When I type just ds; following text is returned:
Object data=[0] baseParams=Object paramNames=Object
Any ideas?
Animal
29 May 2007, 6:09 AM
Yes. That version of ds is not loaded.
violinista
29 May 2007, 6:22 AM
Thank you very much for patience, I will try something else, like Simple store etc.
Regards,
Violinist
sanchez
29 May 2007, 5:19 PM
I have this problem exactly... in my case i have a store
dsCurrentProduct = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
{
url:'source/php/GetQuery.php',
method:'post'
}
),
reader: new Ext.data.JsonReader(
{
root: 'records'
},
[
{name:'id'},{name:'title'}
]
)
});
the file GetQuery.php retuns that
({"totalCount":1,"records":[{"id":"140","name":"some product"}]})I change and load the data in some event because I need get this data
i tried that
var oData=dsCurrentProduct.load({params:{id: '140'}});
alert(oData.records[0]) //ruturn undefined
I just need know how access to data... please help me and I'm sorry my english isn't perfect.
tryanDLS
29 May 2007, 5:33 PM
Please read the doc - load is an asynchronous operation. You cannot access the data immediately after executing the load fn.
Animal
29 May 2007, 11:23 PM
What does the "load" method return? (Hint: the documentation tells you)
Does this return value have a "records" property, or did you just write that code at random?
violinista
29 May 2007, 11:36 PM
Ok, I solved problem, with lot of unselfih help of Animal and others from Ext support: the trick is to hook "load" event of datastore, since it is asynchronous. For my case, that means:
var ds = new Ext.data.Store({ .... });
ds.on("load", function() {
//wait until whole dataStore is loaded, and THEN do everything with ds:
alert(ds.getById(100));
});
ds.load();
For Sanchez's case:
dsCurrentProduct = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
{
url:'source/php/GetQuery.php',
method:'post'
}
),
reader: new Ext.data.JsonReader(
{
root: 'records'
},
[
{name:'id'},{name:'title'}
]
)
});
dsCurrentProduct.on("load", function() {
alert (dsCurrentProduct.data.items[0]);
});
var oData=dsCurrentProduct.load({params:{id: '140'}});
THANKS THANKS THANKS guys, you are GREAT!!!
=D>
sanchez
30 May 2007, 6:58 AM
Thanks, your replay was very fast, i understund, but the main problem is the acces to data 8-| i tried that but something is bad:
remember my response is this
({"totalCount":1,"records":[{"id":"140","name":"some product"}]}) // i want access to id and name
var dsCurrentProduct = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
{
url:'source/php/GetQuery.php',
method:'post'
}
),
reader: new Ext.data.JsonReader(
{
root: 'records'
},
[
{name:'id'},{name:'name'}
]
)
});
to access i tried that:
// -- id --
dsCurrentProduct.data.items[0].id
dsCurrentProduct.data.records[0].id
// -- name --
dsCurrentProduct.data.items[0].name
dsCurrentProduct.data.records[0].name
I have been working with JSON but here
I don't know what's happening :((
note: i hope you can understand because my ingles isn't the best ;).
Animal
30 May 2007, 7:38 AM
1. Wait until the Store is loaded.
2. Use the Store's methods to retrieve Records, and use the Record's methods to get field values. Don't poke about in private data.
sanchez
30 May 2007, 9:54 AM
Well, i will read more the doc but, do you can give me a example, off course if is posible. thanks.
Animal
30 May 2007, 10:01 AM
var dsCurrentProduct = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
{
url:'source/php/GetQuery.php',
method:'post'
}
),
reader: new Ext.data.JsonReader(
{
root: 'records',
totalProperty: 'totalCount',
id: 'id'
},
[ {name:'id'},{name:'name'} ]
)
});
dsCurrentProduct.on("load", function(){
var firstRec = dsCurrentProduct.getAt(0);
alert("Total recs: " + dsCurrentProduct.getTotalCount() +
"\nFirst record id: " + firstRec.id + ", name: \"" + firstRec.get("name") + "\"");
});
sanchez
30 May 2007, 10:04 AM
THANKS, THANKS, THANKS a lot THANKS.
BoltX
10 Jan 2008, 2:39 PM
Ok, please look below - I understand accessing the RECORDS, but how do I in the bottom example get the "someproperty' value? It is driving me absolutely crazy!
Thanks in advance!
var dsCurrentProduct = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
{
url:'source/php/GetQuery.php',
method:'post'
}
),
reader: new Ext.data.JsonReader(
{
root: 'records',
totalProperty: 'totalCount',
id: 'id',
*** someproperty: 'prop' *****
},
[ {name:'id'},{name:'name'} ]
)
});
dsCurrentProduct.on("load", function(){
var firstRec = dsCurrentProduct.getAt(0);
alert("Total recs: " + dsCurrentProduct.getTotalCount() +
"\nFirst record id: " + firstRec.id + ", name: \"" + firstRec.get("name") + "\"");
});
B.
BoltX
10 Jan 2008, 2:43 PM
And if that is the next question, I do pass that value from the server, and it is read properly, and it is in the jsonData when looking in FB, but I need a way to get somehow to assign THAT value to another var.
I am really confused here. :-(
BoltX
16 Jan 2008, 4:35 PM
value_I_was_looking_for = dsCurrentProduct.reader.jsonData.someproperty
simple enough
oshannon
4 Jun 2009, 7:16 AM
This is the same problem that I was having and it seemed like the perfect solution. In my on load function I was just going to loop until the size of the store was greater than one (only only really need the first item in the store before continuing, so that I can set a drop down's default value). So I wrote the following...
var ServerRecordDef = Ext.data.Record.create([{name:'server'}]);
var ServerRecordDef = Ext.data.Record.create([{name:'server'}]);
serverStore = new Ext.data.Store({
totalProperty : 'length',
method: 'GET',
reader : new Ext.data.JsonReader({
totalProperty: "results",
root : "servers"
}, ServerRecordDef),
url : host + '?retrieveServers'
});
serverStore.on('load', function(){
alert("waiting");
while(serverStore.getCount == 0){}
});
serverStore.on('beforeload', function(){
alert("beforeload");
});
serverStore.load();Unfortunately the load method is never hit (and the "waiting" alert is never shown). I put the beforeload function in just to see if it gets hit and it does. Why is the load not being hit but the beforeload is?
I know everything is being loaded properly because it works sometimes when the load is fast enough.
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.