PDA

View Full Version : Load associated model data



wirtsi
2 Sep 2011, 6:16 AM
Hi there,

I've been trying to get this to work for days, still can't figure out if it can't be done or if I'm too stupid ...

I have 2 models, MobileCollection and Frame, both are associated 1:n



mykita.models.Frame = Ext.regModel('Frame',{
fields: {
name: 'id', type: 'int',
name: 'name', type: 'string',
name: 'mobilecollection_id', type: 'int'
},
association: [
{type: 'belongsTo', model: 'MobileCollection'}
]
});


mykita.models.MobileCollection = Ext.regModel('MobileCollection', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
],
associations: [
{type: 'hasMany', model: 'Frame', name: 'frames'}
]
});

Store definition is



mykita.stores.MobileCollections = new Ext.data.Store({
model: "MobileCollection",
autoLoad:true,
storeId: 'mobilecollections',
proxy: {
type: 'ajax',
url: 'frames.json',
reader: {
type: 'json',
root: 'mobile_collections'
}
}
});

and the json I'm trying to load is


{
"success" : true,
"mobile_collections": [
{
"id": 1,
"name": "Optical",
"frames": [
{
"id": 316,
"mobilecollection_id": 1,
"text": "Cyrus"
}, {
"id": 318,
"mobilecollection_id": 1,
"text": "Hmmm"
}, {
"id": 319,
"mobilecollection_id": 1,
"text": "What"
}, {
"id": 320,
"mobilecollection_id": 1,
"text": "New one"
}
],
},
{"id": 2,"name": "Sunny"},
{"id": 3,"name": "Laboratory"},
]
}

Now when I look at mykita.stores.MobileCollections, at first glance this seems to work:

data -> items contains 3 elements ... check
data -> items[1] -> data -> frames contains 0 elements ... check
data -> items[0] -> data -> frames contains 4 elements ... check

But when I look at one of these elements (like data -> items[0] -> data -> frames[0]), all I get is
"mobilecollection_id": 1 ???

So Sencha managed to pull together the association but failed to load the remaining data (like "name", "id") from frames. How can I access this without another ajax request?

Any help would be really appreciated ...

Thanks in advance

wirtsi

wirtsi
5 Sep 2011, 12:18 AM
So even at the danger of talking to myself here ... I think I found a bug:

As with the example given above, I found that all the associated data to MobileCollections lives happily at

data -> items[0] -> raw -> frames

So the association is working correctly! In the learning center (http://www.sencha.com/forum/showthread.php?145873-Load-associated-model-data) the suggested way of accessing model data is

data.store.getAt(0).frames() ... but frames only contains the foreign key, not the rest of the data.

Still, is this some mistake on my behalf or should I file a bug report?

Regards

wirtsi

wirtsi
30 Sep 2011, 1:35 AM
Hi

I thought I share the solution ... it is something that should be made more clear in the docs, I only found this out while looking for something completely different.



mykita.models.Frame = Ext.regModel('Frame',{ fields: {
name: 'id', type: 'int',
name: 'name', type: 'string',
name: 'mobilecollection_id', type: 'int'
},
association: [
{type: 'belongsTo', model: 'MobileCollection'}
]
});




mykita.models.MobileCollection = Ext.regModel('MobileCollection', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'frames', type: 'array'}
],
associations: [
{type: 'hasMany', model: 'Frame', name: 'frames'}
]
});

The associated model field needs to be defined here! Name must be identical to the name in the associations array!

Hoooray!

wirtsi