PDA

View Full Version : Form, JsonReader problem/question



wattee
18 Jan 2010, 5:00 AM
Hi,
im having some difficulties concering form load.
I need to get data from more than one set of array, from different tables.

Question:

Is there any way to add multiple json readers to a form?
like if i had a,

reader: new Ext.data.JsonReader({root: 'results', id: 'id'},
[{name: 's_id', mapping: 'id'},
{name: 's_word', mapping: 'word'}])

Would it be possible to somehow add another root (root: 'results2', id: .....)or something like that to get the reading into the same form?

Problem:

I have multiple tables wich is why i need to make multiple queries, thing is, when i dont set a reader in a form, it doesnt load anything to it, even when im just using original names from the database, json result echos true but i can't get it to the form.

When im using the mapping in form inside the reader it loads perfectly.

Condor
18 Jan 2010, 5:52 AM
Unfortunately, readers aren't that flexible.

You'll have to write your own reader, e.g.


reader: {
read: function(response){
var data = Ext.decode(response.responseText);
return {
success: true,
records: [{data: {
's_id': data.results[0].id,
's_word': data.results[0].word,
'x': data.otherroot[0].otherproperty
}}],
totalRecords: 1
};
}
}

daanlib
18 Jan 2010, 5:56 AM
Maybe it's not really an answer to your question, but it is perfectly possible to have an SQL query return results from multiple tables:



SELECT table_a.*, table_b.*
FROM table_a, table_b
WHERE ...


This way there's no need for multiple readers or roots.

wattee
18 Jan 2010, 6:20 AM
Thank you Condor, appreciate it.

daanlib, unfortunately it wouldn't work since i need to get data from main table + 2 more tables on specific criteria and i also need another query from the same main table + 2 more tables with different criteria resulting 2 different sets of data.

wattee
18 Jan 2010, 10:35 AM
Ah crap, i was cheering before testing. When trying to do:


reader: {
read: function(response){
var data = Ext.decode(response.responseText);
return {
success: true,
records: [{data: {
's_id': data.results[0].id,
's_word': data.results[0].word,
'n_id': data.results2[0].id_second,
'n_word': data.results2[0]word_second.
}}],
totalRecords: 1
};
}
}

It gives me an error: missing ) in parenthetical

Im sure its a result of multiple queries, since testing those they worked fine when commenting out reader data corresponding to the active query, but when trying them both on it fails.

Any ideas?

daanlib
18 Jan 2010, 10:41 AM
there's a syntax error in your js:


'n_word': data.results2[0]word_second.
//should be: 'n_word': data.results2[0].word_second
see the point? ;)

wattee
18 Jan 2010, 11:08 AM
its not syntax error well not in my original code at least :D, since im posting in english speaking forum and my code is mostly estonian i usually alter my copy of the code, like shorten and change words whatever, but i think i got to the problem.

Since i saw so many posts about json here regarding the same error,
i pasted my json result to online parser.
It looks like the error is given because im not combining two results, i had a result:



{"success":"true","tulemused":[{"id":"1","emakeel":"tere","voorkeel":"hello"}]}
{"success":"true","tulemused2":[{"id":"1","oma_knimi":"jojo","inglise_knimi":"jojoenglish"}]}
and i just needed to follow up 1st json data with a comma to get rid of the error, and as i was writing this comment i managed to combine these two results by the comma and removing few braces separating them:



{"success":"true","tulemused2":[{"id":"1","oma_knimi":"jojo","inglise_knimi":"jojoenglish"}],
"success":"true","tulemused":[{"id":"1","emakeel":"tere","voorkeel":"hello"}]}