View Full Version : Are associated relationships not loaded automatically?

12 Feb 2014, 7:19 PM
I created the following JSFiddle example to demo the problem that I am experiencing:

I am creating two models that have a hasMany relationship between them. I am then creating the parent model by parsing a request JSON object that I am getting from my server.

I cannot seem to have any luck getting Ext JS to automatically load and wire up my associations for me.

Please see my JS Fiddle for a working example.

The only workaround that I can find is to load the associations myself manually:


It is also my understanding that I will face a similar issue when it comes to writing out the model.

Can someone confirm if it is indeed true that associations are neither read or written automatically to records as of the current version of ExtJS?


13 Feb 2014, 10:48 AM
It should read fine, unless I understood what you are looking for. Have a look at this example:

You are correct that the writer does not support associated data at this time. You would need to send the data and handle it on the server side.

13 Feb 2014, 1:07 PM
Thanks for the fiddle Scott - it does indeed load associations.

What I gather from your example though, is that to load associations, I need to create a Store. What I was doing was instantiating a Model and expecting associations to be loaded - which wasn't the case.

Example 1 - Model not loading associations

var requestData = JSON.parse(response.responseText); // JSON loaded from server
var record = Ext.Create('ModelName', requestData);

If I was to create a model record with associations using this method, associations are not loaded in the instantiated record.

Example 2 - Store loads Model associations

var requestData = JSON.parse(response.responseText);
var store = Ext.create('Ext.data.Store', {
model: 'ModelName',
proxy: {
// Proxy configuration ...

var record = model.first(); // Associations are successfully loaded

I now understand how to get the reading of associations working (with a Store). It would be great if you can elaborate on what I have observed at the top.

Given that a Model can have a proxy inside of it to read/write data, doesn't it make sense to have the logic to parse associations inside the Proxy instead of the Store?


On a side note - I didn't even know there was a Sencha fiddle; it's pretty awesome! Is it possible that we can host our own Sencha fiddle internally to use for internal documentation of our customisations?

13 Feb 2014, 2:49 PM
Have a look at the following:

Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
'id', 'name', 'total'],

hasMany: {
model: 'Order',
name: 'orders'

proxy: {
type: 'rest',
url: 'data.json',
reader: {
type: 'json',
root: 'users'

Ext.define('Order', {
extend: 'Ext.data.Model',
fields: [
'id', 'total'],

hasMany: {
model: 'OrderItem',
name: 'orderItems',
associationKey: 'order_items'
belongsTo: 'User'

Ext.define('OrderItem', {
extend: 'Ext.data.Model',
fields: [
'id', 'price', 'quantity', 'order_id', 'product_id'],

belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]

var myUser = Ext.create('User', {
id: 123,
name: 'Ed',
orders: [{
id: 50,
total: 100,
order_items: [{
id: 20,
price: 40,
quantity: 2,
product: {
id: 1000,
name: 'MacBook Pro'

console.log(myUser.raw); // raw contains associated data

13 Feb 2014, 3:06 PM
As for fiddle, at this time, it is internal. Also note that you can embed the fiddle in the forum post replacing the 'code' tags with 'fddie' and place the last part of the url inside

// spaces added to show syntax
[ fiddle ] 1jp [ / fiddle ]