PDA

View Full Version : Strange association problem



bozegovic
24 Sep 2013, 10:14 AM
Ext version tested:

Ext 4.2.1 rev 883
Browser versions tested against:

Chrome and Firefox
Description:

I have strange association problem. Nested association works only when I load model for the second, and greater, time. First load usually returns method is not defined, and sometimes returns empty store, although store has items
Steps to reproduce the problem:

Create some mode
Create hasMany association with name: 'relatedItems'
Load parent model with Model.load(id, {...
console.log(record.relatedItems())
The result that was expected:

Record with associated data
The result that occurs instead:

Sometimes relatedItems is not model function
Sometimes empty Store
Test Case:



/**
* User: boris
* Date: 4/1/13
*/
Ext.define('Cms.model.TopList', {
extend: 'Ext.data.Model',
fields: [
{
name: 'id',
mapping: Cms.fields('toplist.id')
},
{
name: 'title',
mapping: Cms.fields('toplist.title')
},
{
name: 'description',
mapping: Cms.fields('toplist.description')
},
{
name: 'coverImage',
mapping: Cms.fields('toplist.coverImage')
},
{
name: 'isPublished',
mapping: Cms.fields('toplist.isPublished')
},
{
name: 'items',
mapping: Cms.fields('toplist.items')
},
{
name: 'publishDate',
mapping: Cms.fields('toplist.publishDate'),
type: 'date',
dateFormat: 'c',
defaultValue: new Date()
},
{
name: 'publishTime',
type: 'date',
dateFormat: 'c',
convert: function(v, record) {
if (record.get('publishDate')) {
return record.get('publishDate');
} else {
return new Date();
}
},
persist: false
}
],
associations: [
{
type: 'hasMany',
model: 'Cms.model.TopListItem',
name: 'relatedItems',
associationKey: Cms.fields('toplist.items')
}
],
proxy: {
type: 'rest',
url: Cms.urls.getUrl('api/toplist'),
reader: {
type: 'json'
},
writer: {
nameProperty: 'mapping'
}
},
validations: [
{type: 'presence', field: 'title'},
{type: 'length', field: 'title', max: 80}
]
});

/**
* User: boris
* Date: 4/1/13
*/
Ext.define('Cms.model.TopListItem', {
extend: 'Ext.data.Model',
fields: [
{
name: 'image',
mapping: Cms.fields('toplistitem.image')
},
{
name: 'description',
mapping: Cms.fields('toplistitem.description')
},
{
name: 'id',
mapping: Cms.fields('toplistitem.id')
},
{
name: 'title',
mapping: Cms.fields('toplistitem.title')
},
{
name: 'url',
mapping: Cms.fields('toplistitem.url')
}
],
associations: [
{
type: 'belongsTo',
model: 'Cms.model.Image',
getterName: 'getImage',
associationKey: Cms.fields('toplistitem.image')
}
],
proxy: {
type: 'rest',
url: Cms.urls.getUrl('api/toplistitem'),
reader: {
type: 'json'
},
writer: {
nameProperty: 'mapping'
}
},
validations: [
{type: 'presence', field: 'title'},
{type: 'length', field: 'title', max: 200}
]
});

/**
* User: boris
* Date: 4/1/13
* Time: 2:53 PM
*/
Ext.define('Cms.model.Image', {
extend: 'Ext.data.Model',
fields: [
{
name: 'id',
mapping: Cms.fields('image.id')
},
{
name: 'title',
mapping: Cms.fields('image.title')
}
],
proxy: {
type: 'rest',
url: Cms.urls.getUrl('api/image'),
reader: {
type: 'json'
},
writer: {
nameProperty: 'mapping',
writeAllFields: false
}
},

validations: [
{type: 'presence', field: 'title'},
{type: 'length', field: 'authors', min: 1}
]
});


{

"description": "opis topliste",
"id": 92,
"is_published": true,
"last_change": "2013-09-24T11:06:36.628900",
"publish_time": "2013-09-24T00:00:00",
"resource_uri": "/backendapi/v1/toplist/92",
"site": "/backendapi/v1/site/1",
"slug": "naslov",
"title": "naslov",
"toplist_items": [
{
"cover_image": {
"id": 116,
"title": "usa"
},
"description": "opis 1",
"id": 21,
"last_change": "2013-09-24T11:06:36.808937",
"order": 1,
"resource_uri": "/backendapi/v1/toplistitem/21",
"title": "element 1",
"toplist": "/backendapi/v1/toplist/92",
"url": "url 1"
},
{
"cover_image": {

"id": 113,
"title": "naslov",
},
"description": "opis 2",
"id": 22,
"last_change": "2013-09-24T11:06:36.814173",
"order": 2,
"resource_uri": "/backendapi/v1/toplistitem/22",
"title": "element 2",
"toplist": "/backendapi/v1/toplist/92",
"url": "url 2"
}
]

}

Gary Schlosberg
24 Sep 2013, 5:14 PM
Thanks for the report. I wasn't able to find any exisiting issues like this. Can you please post a functioning test case so that we can file a bug? Thanks.

bozegovic
25 Sep 2013, 2:47 AM
Ah, I have found the error. Error was that I didn't have defined associated models, that is why it worked on second load, on second load model were used.

But I did get sometimes empty store, and sometimes undefined. I don't know yet why this is happening. I will inspect further.

Tnx anyway.