PDA

View Full Version : How to add associations in Model dynamically



sarasa
11 Nov 2013, 4:33 AM
Hi,

I want to add the associations on model based on the value of a field.
For example:

I want to add associations like

Parent Model:


Ext.define('App.model.parentModel', {
extend: 'Ext.data.Model',
requires: [
'App.model.ChildModel1',
'App.model.ChildModel2',
],
idProperty: 'type',
fields: [
'name',
'type'
],
proxy:[//proxy]
});

I want to add associations like


hasMany:{
model:'App.model.ChildModel1'
}

if my value for 'type' field is automobile.

Else I want to add associations like


hasMany:{
model:'App.model.ChildModel2'
}

How can I check this on fly and add associations?

burnnat
11 Nov 2013, 10:57 AM
The difficulty here is that the associations are managed at the model class level, rather than at the level of individual records, so you have to have the same set of associations across all records for a particular model. Why not add all the associations to the model up front, and then just leave the ones that don't apply as empty in your data? Or use separate models for the different types of records?