Marcel Stör
Marcel Stör

Reputation: 23535

How to map nested JSON into Sencha model with associations?

The associated Sencha Touch model is not mapped from JSON and I have no idea what I'm missing.

Sencha Touch models

Ext.define('MyApp.model.Station', {
  extend: 'Ext.data.Model',

  config: {
    fields: [
      'name',
      {name: 'lat', type: 'float'},
      {name: 'long', type: 'float'}
    ],
    hasMany: {model: 'MyApp.model.Forecast', name: 'forecast'}
  }
});

Ext.define('MyApp.model.Forecast', {
  extend: 'Ext.data.Model',

  config: {
    fields: [
      {name: 'date', type: 'date'},
      {name: 'sunshine', type: 'int'},
      {name: 'temperature', type: 'float'}
    ],
    belongsTo: 'MyApp.model.Station'
  }
});

JSON

{
    "name": "some name",
    "lat": 11.5716,
    "long": 4.68715,
    "forecast": [
        {
            "date": "2013-09-20 13:00:00",
            "sunshine": "11"
        },
        {
            "date": "2013-09-20 14:00:00",
            "sunshine": "19"
        }
    ]
}

The simple fields are mapped just fine but the forecast array remains undefined. If I remove the Forecast model completely and just declare forecast as a simple field (see the following code snippet) the mapping works as expected. Any ideas why?

Ext.define('SunApp.model.Station', {
  extend: 'Ext.data.Model',

  config: {
    fields: [
      'name',
      {name: 'lat', type: 'float'},
      {name: 'long', type: 'float'},
      'forecast'
    ]
  }
});

Of course a working "solution" is better than nothing but the drawback is that I don't have a declared and thereby documented forecast model.

Upvotes: 2

Views: 5152

Answers (1)

arthurakay
arthurakay

Reputation: 5651

Perhaps you're missing the associationKey configuration. There are definitely some quirks in how the associations work!

Upvotes: 1

Related Questions