PDA

View Full Version : Load local data for model



Cp_Barbossa
27 Sep 2012, 4:01 AM
Hello.

I wander, is there any way to load local data (local object) for model and for its associations?

For example, i have two models:

Ext.define('application.model.PersonModel', {
extend: 'Ext.data.Model',
idProperty: 'ID',
fields: [{
name: 'ID',
type: 'int'
}, {
name: 'Name',
type: 'string'
}],
associations: [{
type: 'hasMany',
model: 'application.model.ContactModel',
name: 'Contacts'
}]
});

and

Ext.define('application.model.ContactModel', {
extend: 'Ext.data.Model',
idProperty: 'ID',
fields: [{
name: 'ID',
type: 'int'
}, {
name: 'PersonID',
type: 'int'
}, {
name: 'Mobile',
type: 'string'
}],
associations: [{
type: 'belongsTo',
model: 'application.model.PersonModel',
setterName: 'setPerson',
getterName: 'getPerson',
primaryKey: 'ID',
foreignKey: 'PersonID',
associationKey: 'Person',
associatedName: 'Person'
}]
});

So, lets say somewhere in my .js file i have just simple object like this:

var myObject={
ID: 1,
Name: 'Cp_Barbossa',
Contacts: [{
ID: 1,
PersonID: 1,
Mobile: '123456'
}, {
ID: 2,
PersonID: 1,
Mobile: '654321'
}]
};

So, I tried

var pm = Ext.create('application.model.PersonModel', myObject);
It really fills Person model, but not it's association - Contact model.

Finally, what i want is to load model (this case Person model) and it's associations (this case Contact model) from local object (like myObject);

Is there any way to do this?

P.S. If i response myObject similar data from server, using proxy, it works fine - it fills Person model and it's Contacts too.

redraid
27 Sep 2012, 4:37 AM
try:


var pm = Ext.create('application.model.PersonModel', myObject);
// Add contacts to assoc store
pm.Contacts().add(myObject.Contacts);