PDA

View Full Version : Problems using a simples Ext.data.Store and Json



angraxs2
19 Dec 2011, 6:04 AM
Hi, iīve been looking all over for some answer about this, but i couldnīt find it anywhere. Im reading the API Docs for the thousand time, but it is just like mine! I just canīt see what Iīm doing wrong.

Iīm trying to use the Ext.Data.Store using a json parse to get 1 single information. Here is my code:


Ext.define('MenusLeftModel', {
extend: 'Ext.data.Model',

fields: [

{name: 'menus', type: 'string'},
]
});

var menus = Ext.create('Ext.data.Store',
{
model: 'MenusLeftModel',

proxy: {

type: 'ajax',

url : '../portal/php/menus.php',

reader: {

successProperty: 'success',

type: 'json',

root: 'config',

}

},

/*data : [

{'menus': '23456'},

],*/

autoLoad: true,

});

console.log('MENUS ---> ' + menus.getAt(0).get('menus'));


My PHP is returning a valid json string:


{ "success": true, "config": [{"menus": "235689"}] }

If I remove the comments on the Data config, it shows it but not the json data. I think its not reading my json string...

As you can see, its a simple code, with a simple function, but I cant get it to work with my PHP. Any help? Thanks in advance

mitchellsimoens
19 Dec 2011, 9:56 AM
Looking at this code:


Ext.define('MenusLeftModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'menus', type: 'string'},
]
});

var menus = Ext.create('Ext.data.Store',
{
model: 'MenusLeftModel',
proxy: {
type: 'ajax',
url : '../portal/php/menus.php',
reader: {
successProperty: 'success',
type: 'json',
root: 'config',
}
},
/*data : [
{'menus': '23456'},
],*/
autoLoad: true,
});

console.log('MENUS ---> ' + menus.getAt(0).get('menus'));

The store is setup properly for your JSON. The console.log will fire right after the store is created but the loading is still happening. You need to add a listener for the load event and do the console.log there:


Ext.define('MenusLeftModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'menus', type: 'string'},
]
});

var menus = Ext.create('Ext.data.Store', {
model: 'MenusLeftModel',
proxy: {
type: 'ajax',
url : '../portal/php/menus.php',
reader: {
successProperty: 'success',
type: 'json',
root: 'config',
}
},
autoLoad: true,
listeners : {
load : function(store) {
console.log(store.getAt(0).get('menus'));
}
}
});

Now that should work for you.

angraxs2
19 Dec 2011, 10:13 AM
Thank you :)

I thought it was a problem on my JSON. Now i can set my menus properly!


Thanks again;