This is a two-parter question here. The first is a technical question about how Sencha parses JSON and reads stores.
I have two models, Order and User:
Ext.regModel('Order', {
fields: [
{name: 'id', type: 'int'},
{name: 'user_id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'price', type: 'float'},
],
belongsTo: 'User'
});
Ext.regModel('User',{
fields: [
{name: 'id', type: 'int'},
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'},
{name: 'email', type: 'string'},
],
associations:[
{type: 'hasMany', model: 'Order', name: 'orders'}
]
});
Overall, a pretty simple association. I have the following JSON that is retrieved from the server:
[
{
"id":22,
"price":0.0,
"name":"My First Order",
"user_id":3,
"user": {
"id": 3,
"first_name": "Michael",
"last_name": "Jones",
"email": "[email protected]"
}
}
]
Lastly, here is my store:
var o = new Ext.data.Store({
model: "Order",
sorters: "name",
getGroupString: function(record){
return record.get('name')[0]
},
proxy: {
type: 'ajax',
url: '/orders',
headers: {'Accept': 'application/json'},
reader: {
type: 'json'
}
},
autoLoad: true
});
Ext.regStore("Orders",o);
When I read the order data, I can't find any mention of the user. So, then I found this handy guide in the Sencha documentation: http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.Reader
However the documentation only shows an example for a hasMany association - it doesn't include one pulling data from a belongsTo association. I tried scouring the internet for a couple hours and came up empty-handed. What is the proper way to retrieve the user field from an order field?
UPDATE: I removed the proxy and autoLoad parameters in my store and replaced it with a data parameter, which contained the same exact string of JSON returned by my server in my example. And guess what? It worked. However, I still need to fix this problem so that it will work when communicating with my server! Any ideas?
And the second part: What are your thoughts about Sencha Touch? The framework seems very powerful, but the documentation and examples seem rather weak. I'm also concerned by how difficult parsing this data has become. Are there better alternatives out there?