PDA

View Full Version : ArrayStore with JSON



Nawabu
20 Oct 2009, 11:00 AM
Hi,

I try to fill an ArrayStore with a JSON Objekt. But when I try indizes.getCount() it throws only 0.
Here is my Code:



indizes = new Ext.data.ArrayStore({
fields: ['type', 'number'],
url: 'loadData.php?param=index',
idIndex: 0,
autoLoad: true
});And this is the JSON Objekt:



{success: true,
data:[
{type:"sprache", number: 2},
{type:"studium", number: 2},
{type:"praktikum", number: 3},
{type:"ausland", number: 0}
]
}

I am very new to ExtJS please help me someone out. The next question will be, how can I reach the elements in the best way?

thanks
Nawabu

carol.ext
20 Oct 2009, 11:05 AM
Take a look at the example data for ArrayStore (http://www.extjs.com/deploy/dev/docs/?class=Ext.data.ArrayStore) and JsonStore (http://www.extjs.com/deploy/dev/docs/?class=Ext.data.JsonStore) in the API. You're not really hitting either one.

Mike Robinson
20 Oct 2009, 11:07 AM
You should be using a JsonStore object here, not an ArrayStore.

(By the way, a JsonStore can also have a data property to supply [test...] data directly.)

The essential way that you use these objects is as follows:


If you need for the dataset to be writeable as well as readable, you'll need to also associate a JsonWriter object with the store.
Basically, you issue a load() request, or reload() as the case may be.
Upon return from this call, the requested data is not yet available! It might not even have been requested yet. But at some time in the future it will be requested, and if that request is successful a load event will be fired. (But of course the request might fail. The load-event might never come ...)
Use the listeners facility to respond appropriately when the event fires.
After you know that the data is there, methods such as getAt() will retrieve the Ext.data.Record objects. The record-hash is in their data member.


Patiently and carefully study the source-code of all of the demos which bear any resemblance at all to what you are setting out to do. (You say you already did that? Great! Do it again!) :)

Nawabu
20 Oct 2009, 1:03 PM
Hi,

sorry for my stupid questions. But here is the next one ;)
My code works now. But I dont understand the listeners. How can I wait for the request before I go on? I tried it with autoload or with the .load() function.

thanks

Nawabu
21 Oct 2009, 3:44 AM
Hi,

I tried it with the following code:



indizes = new Ext.data.JsonStore({
fields: ['type', 'number'],
url: 'loadData.php?param=index',
root: 'data',
idProperty: 'type'
});

indizes.load({
waitMsg : 'Loading Data...',
success: function(){
alert("Test 1");
},
failure: function() {
alert("Test 2");
}
});


But neither the watMsg nor the alerts are executed. But the data is loaded correctly.

Please tell me somenone my mistakes. I just started to work with javascript an ajax and habe no idea.

thanks