PDA

View Full Version : How can i catch values from a store



Maxime
6 Aug 2009, 1:46 AM
Hello,

I'm trying to get a value from this json file:

{success:true, rows:[{"id":3,"civilite":"civilite","nom":"nom","fact_adresse":"fact_adresse","fact_adresse2":"fact_adresse2","fact_cp":"fact_c","fact_ville":"fact_ville","fact_interlocuteur":"fact_interlocuteur","fact_telephone":"fact_telephone","fact_fax":"fact_fax","fact_portable":"fact_portable","fact_email":"","livr_adresse":"livr_adresse","livr_adresse2":"livr_adresse2","livr_cp":"livr_c","livr_ville":"livr_ville","livr_interlocuteur":"livr_interlocuteur","livr_telephone":"livr_telephone","livr_fax":"livr_fax","livr_portable":"livr_portable","livr_email":"[email protected]","catalogue":"catalogue","id_revendeur":"[email protected]","date":"2009-08-06 08:28:34","derniere_visite":null}]}

I need to get "id" into a javascript var.
For example :

var my_own_id = (data from json file);
alert(my_own_id);

In order to do that, i created an ExtJS datastore:


var store2 = new Ext.data.Store({
url: 'clients.json',
reader: new Ext.data.JsonReader({
root:'rows',
id:'id'
},[
'id',
'civilite',
'nom',
'fact_cp',
'fact_ville',
'fact_interlocuteur',
'fact_telephone',
'fact_fax',
'fact_email',
'catalogue',
'derniere_visite'
])
});
store2.load();

Now, i've to ask to datastore my var, but i don't know how i can do that :(
First, i think i've to select my row, and after my field, but how ?

Thanks for your help.

evant
6 Aug 2009, 1:53 AM
Have a look at the docs for the store, there's various methods for grabbing a record.

Once you have the record, use the get() method on that.

Maxime
6 Aug 2009, 3:54 AM
With my example, I tried this:
alert(store2.getCount());
alert(store2.getTotalCount());

I got 0. I tried to get some data with store2.getById and store2.getAt, but i got undefined.
I not understand something. I shouldn't use right syntax :(

Animal
6 Aug 2009, 4:07 AM
Did you read the docs?

Especially the bold bit?

http://i131.photobucket.com/albums/p286/TimeTrialAnimal/loaddocs.jpg

moegal
6 Aug 2009, 4:21 AM
You need to make sure the store is loaded first. You could add a load listener and then run your code or call a function from there.



store2.store.on(
'load', function(){
//code here
});


Then you could something like:

store2.data.items[0].id

Thanks,
Marty

Maxime
6 Aug 2009, 4:57 AM
Sorry, my english may be not enough fluent to undestand all subtlety. I do mistake, it's sure, but i'm trying to understand :">

So, if i understand you, Animal, you told my json file isn't loaded ?
So, i should use something like this:

store2.load({
callback: alert(XXX)
});

I tried to replace XXX by store2.getCount(), or store2.data.items[0].id like Moegal told, but i didn't get expected data.

Thanks.

Animal
6 Aug 2009, 5:05 AM
Don't alert.

DEBUG.

You will not get anywhere until you learn to debug.

Set a breakpoint there, and look around at what you've got.

moegal
6 Aug 2009, 5:13 AM
I use firefox with firebug and use console.log() to see the object.

so I would run something like this from the firebug console after the page is loaded:

console.log(store2)

you can click on the object and see where everything is.

Marty

Animal
6 Aug 2009, 5:28 AM
You still don't debug after all this time here?

Yuo still have another 50% efficiency improvement to go on your ExtJs development abilities then!

Maxime
6 Aug 2009, 5:30 AM
I see something for store2.fields.items, but it's empty when i'm looking into store2.data.items. So my datastore isn't nicely created i think, it's not a reading data problem :-?

moegal
6 Aug 2009, 5:54 AM
I could use that extra efficiency about now. Is there a tutorial on debugging?
Thanks, Marty

rehanazher
6 Aug 2009, 6:07 AM
store2.on("load", function() {
myRecord = sotre2.getAt(0);
var a =myRecord.get('civilite');
alert(a);
});
would give you your first record value for column names civilite.

I hope it helps,

thanks,

steffenk
6 Aug 2009, 6:10 AM
I could use that extra efficiency about now. Is there a tutorial on debugging?
Thanks, Marty

Just do what Animal suggested. Set a breakpoint in Firebug
(script tab, choose your script, click on the linenumber where to stop (red dot))

When script stopped, use the inspect window to see what is available at this time.

carol.ext
6 Aug 2009, 6:22 AM
On Firebug debugging:

http://getfirebug.com/js.html

Ext FAQ on Firefox/Firebug (http://extjs.com/learn/Ext_FAQ_Debugging#Firefox.2FFirebug)

Or use google.

Different tutorials and help pages show different versions.

moegal
6 Aug 2009, 7:50 AM
thanks,
Marty

Maxime
6 Aug 2009, 10:08 PM
Thanks rehanazher (http://extjs.com/forum/member.php?u=81197), it works.
I think i've understood the rule. I'll try to do something like that by myself with an other example.

Many thanks to all for your help.