PDA

View Full Version : Why doesn't Ext.create('Model', ...) load associated model data?



ykey
23 Aug 2011, 5:25 AM
I am having trouble loading associations through Ext.create. The following works fine when data is retrieved through a server proxy. However when the same data is loaded through Ext.create no associations are created.

The UI component depends on the nested stores and I would like to have default data available if the AJAX call fails or times out and for testing purposes.



Ext.define('Server', {
extend: 'Ext.data.Model',
idProperty: 'name',
fields: [
'name'
],

hasMany: {
model: 'Container',
name: 'containers'
}
});

Ext.define('Container', {
extend: 'Ext.data.Model',
idProperty: 'name',
fields: [
'name'
]
});

var sys = Ext.create('Server', {
"name": "Server 1",
"containers": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
]
});

console.log(sys); // Has 3 containers in the data field
console.log(sys.containers()); // Has 0 records

skirtle
23 Aug 2011, 7:21 PM
I agree that what you're trying to do appears sensible but it doesn't work. My guess is that associations are handled at the reader level and using Ext.create() skips that. It seems very counter-intuitive to me.

It took me quite a while to find a way of doing it that actually worked. Maybe there's a better way but this is what I came up with:


var reader = Ext.create('Ext.data.reader.Json', {
model: 'Server'
});

var resultSet = reader.read({
"name": "Server 1",
"containers": [
...
]
});

var sys = resultSet.records[0];

All feels a bit yucky to me. I don't see anything there that couldn't be done implicitly in the model's constructor.

devnullable
25 Aug 2011, 7:01 AM
I feel your pain guys. I was just wondering what's going on when associations are not loaded on Ext.create. Working on anything more "complex" than in examples, like a form with grids of associated model data is really frustrating experience. Build JSON by hand for saving primary model AND association models in same atomic AJAX request and then try to figure out how to load back response again... :-/

skirtle
25 Aug 2011, 6:16 PM
I've got this on my list of ExtJS 4.0 issues. I believe 4.1 addresses a lot of the shortcomings of 4.0 so I'm waiting for that release before I throw in a 'feature request' for this.

mberrie
13 Sep 2011, 6:35 AM
Is there a bug report for this? I wouldn't expect 4.1 to fix an issue just because it is obvious that a feature is incomplete. ;)

skirtle
13 Sep 2011, 6:51 AM
I've seen comments from the Sencha devs that 4.1 will improve the data model, particularly associations. I don't know the details but I can't be bothered spending hours filing reports on issues until I know what's in 4.1.

I think the word bug may be a little strong in this case.

S├ębastien.Volle
18 Apr 2012, 5:19 AM
From what I've seen in 4.1 RC3, Ext.create('Model', ...) or new Model(...) still does not create association data in the Model instance. One still have to go through a reader for the associated data to be built.

Is there any plan for adding that in 4.1 GA? When instanciating a model instance, when all the required data is provided, I don't see why association data should not be there in the instance.

gjuggler
29 May 2012, 9:46 PM
I've started running into this problem too.

It seems silly that associations can be loaded from serialized formats via the JSON and XML reader classes, but not from an in-memory data structure via the standard Ext.create() call. It's an unfortunate oversight in the API design.

I'd love to see this fixed in a future release!

dougi
2 Aug 2012, 1:38 AM
i thought first that it was a bug on my side but it's worst. Can we expect a correction soon?

jcervenka
2 Aug 2012, 8:06 PM
This works:
http://stackoverflow.com/questions/10104739/populating-associated-models-with-nested-json (http://stackoverflow.com/questions/10104739/populating-associated-models-with-nested-json)

Qanik
13 Nov 2012, 11:15 PM
This is REALLY counter-intuitive. Are there any plans to improve Ext.create(..) to load nested data?

matthewdfleming
4 Feb 2013, 2:11 PM
We had this same problem in sencha touch.. the solution was like this..

This code is in a button click handler for a 'create new assessment' button. The idea is that a create call is supposed to be made to the server and the result of that call should be an 'Assessment.' The json stream coming back has entries for all of the related items (e.g. categories, assessor, etc).



var me = this;


// create a new assessment with the client and tool
var newOne = Ext.create('helium.model.Assessment');


// save it so that the 'create' call will happen on the server
newOne.save({
success: function (record) {
// for some reason the 'create' flow doesn't resolve associations
// need to manually push through a reader
var reader = Ext.create('Ext.data.reader.Json', {
model: 'helium.model.Assessment'
});
var resultSet = reader.read(record.data); //the record itself won't work, just the .data portion
var assessment = resultSet.getRecords()[0];
// 'assessment' is now fully read in with associations
},
scope: me
});


Here's the model object (using a REST proxy).. the writer is a custom writer that will also push all of the related items down on a save/update call..


Ext.define('helium.model.Assessment', {


extend: 'Ext.data.Model',
config: {
useCache: false,
fields: [
'id', 'version', 'startDate', 'type',
{name: 'allowedActions', persist: false},
{name: 'status', persist: false},
{name: 'baseType', persist: false},
{name: 'major', persist: false},
{name: 'minor', persist: false}


],
hasMany: [
{model: 'helium.model.Category', name: 'categories'}
],
hasOne: [
{model: 'helium.model.Assessor'},
{model: 'helium.model.OrgUnit'},
{model: 'helium.model.Client'}
],
proxy: {
type: 'rest',
writer: Ext.create('helium.util.IncludeRelationshipJsonWriter'),
api: {
create: '../data/assessment/create',
read: '../data/assessment/read',
update: '../data/assessment/update',
destroy: '../data/assessment/delete'
},
reader: {
type: 'json',
rootProperty: 'assessment'
}
}
}
});

hdave
3 Feb 2014, 4:14 PM
Yet another person that found this thread. I am using 4.2.2 and am frankly stunned that this doesn't work. I really hate the idea of creating a reader just to do this....

adrian_crouch
5 Jun 2014, 7:15 AM
It neither seems to work with extjs 5 .. :(