PDA

View Full Version : Load data to the store via httpProxy



nazol
10 Mar 2012, 6:19 AM
Hello! I have MySQL test database with movies and want to load data from there and display them into the combobox
I've created a store and reader like this


var genres = new Ext.data.Store({
reader:new Ext.data.JsonReader({
fields:['id','genre_name'],
root:'rows'
}),
proxy: new Ext.data.HttpProxy({
url: 'genres.php'
})
});

sufficient part of 'genres.php' page is looks like this


$query="SELECT * FROM genres";
$result=mysql_query($query);
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
Echo '{rows:'.json_encode($arr).'}';

If I run this page in Firefox, I get the following output


{rows:[{"id":"1","genre_name":"Comedy"},{"id":"2","genre_name":"Drama"},{"id":"3","genre_name":"Action"},{"id":"4","genre_name":"Mystery"}]}

now I'm trying to load data into the store


genres.load();

And I think I have error right here. I 've set a break point at the last line of code and when FireBug stops on it, I see, that there is no lines in the genres. Just to be safe I tried to use genres store in the combobox


{
xtype: 'combo',
hiddenName:'genre',
fieldLabel:'Genre',
mode:'local',
store:genres1,
displayField:'genre_name',
valueField:'id',
width:200
}

But when I clicked combobox, all what I got is the spinning wheel and 'Loading' text.
I don't understand how


genres.load()

works. Is it goes to the 'genres.php' script and retrieves data from it's Echo call? And what's happening next? I am a bit newby in this techonology, so please, sorry for a maybe dumb questions.

Farish
10 Mar 2012, 9:09 AM
try changing your model and store to this style:





Ext.define (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext-method-define)("User", {
extend: 'Ext.data.Model (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.Model)',
fields: [
'id', 'name'
]
});

Ext.create (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext-method-create)('Ext.data.Store (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.data.Store)', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
});

Note that the reader is part of the proxy here and you just define the type using the type config in the reader.

Next. see the console of Firebug in Firefox. It shows you the requests and responses. you can see if the store actually sent a request and what the response was. You may also try the following to see the records in the store:


genres.on('load', function() {
console.log(genres.data)
});

nazol
10 Mar 2012, 10:06 AM
Farish (http://www.sencha.com/forum/member.php?277826-Farish), thank you a lot, your suggestion to change model and store hellped me to solve the problem! Thank you!

Farish
10 Mar 2012, 10:08 AM
glad i could help. you dont necessarily need to define a model separately. you may use the fields config in the store directly (but not in the reader as you had done in the original post).