PDA

View Full Version : Conditional fields in Model



varunjyoti
18 Feb 2012, 10:55 AM
Hi,

I have a model


Ext.define('DG.model.Status', {
extend: 'Ext.data.Model',
fields: [

{name: 'gnNumAgntCon', mapping: 'generalStatus.noOfAgentsConnected'},
{name: 'gnNumDataCon', mapping: 'generalStatus.noOfConnections'},
{name: 'nmTotTaskDef', mapping: 'subStatus.FinditTasks.noOfTasks'},

]
});

and a store


Ext.define('DG.store.Statuses', {
extend: 'Ext.data.Store',
model: 'DG.model.Status',
proxy: {
type: 'ajax',
url: 'http://'+hostIP+'/dgadmin/data/statuses.json',
reader: {
type: 'json',
root: 'result',
successProperty: 'success'
}

}
});

Please note that there is a "nmTotTaskDef" fiield in model with mapping "subStatus.FinditTasks.noOfTasks". When i load the store and if subStatus.FinditTasks object does not exist in the JSON, an error is thrown saying "obj.subStatus.FinditTasks" is undefined.

So i want that error should not occur irrespective of the existance of any particular JSON propery/object in the data. This object may or may not be present in JSON.

Thanks

el_chief
18 Feb 2012, 2:35 PM
had this same problem

use the convert() function

test if record.substatus && record.substatus.FinditTasks && record.substatus.FinditTasks exist, and if so, return record.substatus.FinditTasks.noOfTasks, else return null or empty string