PDA

View Full Version : Having issues getting associated data with a hasMany model



McQuack_82
18 Aug 2012, 12:51 PM
I am having issues getting the associated data with a hasMany relation model. I get the data for the parent but am unable to get the children's data


Ext.onReady(function () {

Ext.define('FormFieldsModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'isLocked', type: 'boolean' },
],
associations: [{
type: 'hasMany',
model: 'CustomFieldModel',
name: 'customFields'
}]
});


Ext.define('CustomFieldModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'fieldLabel', type: 'string' },
{ name: 'readOnly', type: 'boolean' },
{ name: 'value', type: 'string' },
{ name: 'allowBlank', type: 'boolean' }
]
});


//json data for test


var data = {
"isLocked": false,
"customFields": [{
"name": "test1",
"fieldLabel": "test1",
"readOnly": false,
"value": "Test1",
"allowBlank": true
}, {
"name": "test2",
"fieldLabel": "test2",
"readOnly": false,
"value": "Test2",
"allowBlank": true
}]
};


Ext.define('SomeFormStore', {
extend: 'Ext.data.Store',
storeId: 'someFormStore',
data: data,
autoLoad: true,
model: 'FormFieldsModel'
});


var store = Ext.create('SomeFormStore');


//store.on('load', function (store) {
// console.log(store);
//});


//store.loadData(fieldData);


//console.log(store);


Ext.Function.defer(function () {
console.log(store);
console.log(store.getAt(0).getAssociatedData()); // No data being loaded
console.log(store.getAt(0).customFields()); // Store is being generated but no data loaded
//console.log(store.getAt(0));
//console.log(store.getAt(0).customFields());
}, 5000);


});

vietits
19 Aug 2012, 12:27 AM
Try to config proxy for store:


Ext.define('SomeFormStore', {
extend: 'Ext.data.Store',
storeId: 'someFormStore',
data: data,
autoLoad: true,
proxy: 'memory',
model: 'FormFieldsModel'
});
var store = Ext.create('SomeFormStore');
console.log(store, store.getAt(0).customFields());

McQuack_82
19 Aug 2012, 11:13 AM
That's interesting cause I thought defaulted proxy type was a memory type. And it is more interest some of the upper level data in the json works but not data needed for the hasMany model. Anyways thanks for the help.

vietits
19 Aug 2012, 3:08 PM
defaultProxyType which is 'memory' only be used when you define fields but don't define model and proxy for your store. In case you define model but don't define proxy, your store will use proxy from your model which is default set to 'ajax'.