Code:
Ext.define('fullhouse.model.accountModel', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.association.HasOne',
'Ext.data.association.HasMany'
],
uses: [
'fullhouse.model.user',
'fullhouse.model.addressModel',
'fullhouse.model.contactModel',
'fullhouse.model.branchModel',
'fullhouse.model.sourceModel'
],
idProperty: 'uid',
fields: [
{
name: 'status',
type: 'string'
},
{
name: 'uid',
type: 'string'
},
{
name: 'accountType',
type: 'string'
},
{
name: 'gender',
type: 'string'
},
{
name: 'lastName',
type: 'string'
},
{
name: 'phone',
type: 'string'
},
{
name: 'email',
type: 'string'
},
{
name: 'organization',
type: 'string'
},
{
name: 'accountManager',
type: 'auto'
},
{
name: 'fax',
type: 'string'
},
{
name: 'firstName',
type: 'string'
},
{
name: 'initials',
type: 'string'
},
{
name: 'memo',
type: 'string'
},
{
name: 'middleName',
type: 'string'
},
{
name: 'mobile',
type: 'string'
},
{
name: 'organization2',
type: 'string'
},
{
name: 'website',
type: 'string'
},
{
name: 'branch',
type: 'auto'
},
{
name: 'source',
type: 'auto'
},
{
name: 'addresses',
type: 'auto'
},
{
name: 'contacts',
type: 'auto'
}
],
hasOne: [
{
associationKey: 'accountManager',
model: 'fullhouse.model.user',
foreignKey: 'accountManager',
getterName: 'getAccountMgr',
setterName: 'setAccountMgr'
},
{
associationKey: 'branch',
model: 'fullhouse.model.branchModel',
foreignKey: 'branch',
getterName: 'getBranch',
setterName: 'setBranch'
},
{
associationKey: 'source',
model: 'fullhouse.model.sourceModel',
foreignKey: 'source',
getterName: 'getSource',
setterName: 'setSource'
}
],
hasMany: [
{
associationKey: 'addresses',
model: 'fullhouse.model.addressModel',
primaryKey: 'uid',
foreignKey: 'addresses',
name: 'addresses'
},
{
associationKey: 'contacts',
model: 'fullhouse.model.contactModel',
primaryKey: 'uid',
foreignKey: 'contacts',
name: 'contacts'
}
]
});
This my 'Save' code
Code:
var form = button.up('form');
var values = form.getValues();
var accountType = form.getForm().findField('accountType').getValue();
var accountStore = this.getAccountStoreStore();
//Get form values
var record= form.getRecord();
form = form.getForm();
//Set record values
form.updateRecord(record);
record.data.addresses = null;
var addresses = record.addresses();
var address = form.findField('visitorAddress').getValue();
var houseNumber = form.findField('visitorHouseNumber').getValue();
var postalCode = form.findField('visitorPostalCode').getValue();
var city = form.findField('visitorCity').getValue();
var country = form.findField('visitorCountry');
var countryData = country.findRecordByValue(country.getValue());
if(postalCode !== null && city !== null && countryData !== null)
{
var addressModel = this.getAddressModelModel().create({addressType : 'VisitorAddress', address : address, houseNumber : houseNumber, postalCode : postalCode, city : city, country : countryData, status:'Active'});
//record.data.addresses = [addressModel];
addresses.add(addressModel);
if(accountType === "Organization")
{
addressModel = null;
address = form.findField('postalAddress').getValue();
houseNumber = form.findField('postalHouseNumber').getValue();
postalCode = form.findField('postalPostalCode').getValue();
city = form.findField('postalCity').getValue();
country = form.findField('postalCountry');
countryData = country.findRecordByValue(country.getValue());
if(postalCode !== null && city !== null && countryData !== null)
{
addressModel = this.getAddressModelModel().create({addressType : 'PostalAddress', address : address, houseNumber : houseNumber, postalCode : postalCode, city : city, country : countryData, status:'Active'});
//record.data.addresses.push(addressModel);
addresses.add(addressModel);
}
}
record.data.addresses = addresses;
}
record.data.contacts = null;
//Check if the record is new. If the record is new add it to store.
if(record.phantom)
{
accountStore.add(record);
}
//Create or update record
accountStore.sync();