PDA

View Full Version : Models, associations and loading nested JSON Data



Urkman
2 Sep 2013, 7:43 AM
Hello,

When I try to set the Data for my models and the associations using nested JSON Data all my associations are empty.
I try to minimize my code as much as possible...

User Model:


Ext.define('Test.model.User', { extend: 'Ext.data.Model',
requires: ['Test.model.Address'],
fields: [ ],
associations: [{ type: 'hasOne', model: 'Test.model.Address', associationKey: 'address', getterName: 'getAddress'}],
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}});


Address Model:


Ext.define('Test.model.Address', {
extend: 'Ext.data.Model',
fields: [
{ name: 'street' },
{ name: 'zipcode' },
{ name: 'city' }
],
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}});


And then in my controller I try to create a new object and set the data:


var test = Ext.create('ZRAS.model.ExamineApplication', "{address: {street: 'street', zipcode: 'zipcode', city: 'city'}}");

console.log(test.getAddress());

And here the output of the address is empty. I get an address object, but no data is set...

And when I remove the memory proxy, I get this error:

Ext.Error: You are using a ServerProxy but have not supplied it with a url.


Where is my problem here?

Thanks,
Urkman

Urkman
2 Sep 2013, 11:19 PM
Hello,

I created a jsFiddle for this:
http://jsfiddle.net/sj9H9/1/ (http://jsfiddle.net/sj9H9/)

Hope someone can help me out here :)

Thanks,
Urkman

Urkman
2 Sep 2013, 11:44 PM
Here is an update to the jsFiddle. With this line it is working:
http://jsfiddle.net/sj9H9/2/

But this is not working with ExtJS 4.2:
http://jsfiddle.net/sj9H9/2/

Why?

ettavolt
4 Sep 2013, 1:48 AM
Because only Readers understand nested associations. .set() changes fields only.
You may want to use model.getProxy().getReader().readRecords(object).

pscanlon1
7 Jan 2015, 9:35 AM
Because only Readers understand nested associations. .set() changes fields only.
You may want to use model.getProxy().getReader().readRecords(object).

This doesnt work. Any other suggestions?