PDA

View Full Version : Model associations in ExtJS



kornicameister
3 Oct 2012, 11:36 AM
Hi.

I believe that my problem is strictly related to ExtJS, although there are some issues on the server side.
But first thing first.
Here are my models:


Ext.define('WMS.model.entity.Warehouse', {
extend: 'Ext.data.Model',


fields : [
'id',
'name',
'description',
{ name: 'maximumSize', type: 'int', defaultValue: 0},
{ name: 'size', type: 'int', defaultValue: 0},
{ name: 'createdDate', type: 'date', serialize: serializeDate}
],
associations: [
{name: 'units', type: 'hasMany', model: 'WMS.model.entity.Unit'}
],


proxy: {
type: 'wms',
url : 'wms/agent/warehouse'
}
});
Ext.define('WMS.model.entity.Unit', {
extend: 'Ext.data.Model',


fields : [
'id',
'name',
'description',
{ name: 'size', type: 'int', defaultValue: 0},
{ name: 'maximumSize', type: 'int', defaultValue: 0}
],
associations: [
{name: 'type', type: 'hasOne', model: 'WMS.model.entity.UnitType'},
{name: 'warehouse', type: 'belongsTo', model: 'WMS.model.entity.Warehouse'}
//{name: 'products', type: 'hasMany', model: 'WMS.model.entity.Product'}
],


//TODO validations


proxy: {
type: 'wms',
url : 'wms/agent/unit'
}
});
Ext.define('WMS.model.entity.UnitType', {
extend: 'Ext.data.Model',


fields : [
'id',
'name' ,
'abbreviation',
'description',
{ name: 'parentType', type: 'int'}
],
associations: [
{name: 'units', type: 'belongsTo', model: 'WMS.model.entity.Unit'}
],
validations : [
{ name: 'length', field: 'name', min: 5, max: 20},
{ name: 'length', field: 'abbreviation', min: 1, max: 6},
{ name: 'length', field: 'description', min: 1, max: 120}
],


proxy: {
type: 'wms',
url : 'wms/agent/unittype'
}


});


And now. I am able to create new entity.Warehouse, because I am not assigning any entity.Unit to it at the moment of creation.
The problem is when I try to create new unit. As you can see Unit is defined with two associations with UnitType (hasOne) and Warehouse(belongsTo).

now, following code does the trick with setting reference to warehouse


var unit = Ext.create('WMS.model.entity.Unit',model);
Ext.getStore('Warehouses').getWarehouse().units().add(unit);

but still I have no reference to UnitType.

So as one of my first question in this thread I would like to ask about this:
What's the valid way to set reference to UnitType in Unit model ?