PDA

View Full Version : Dealing with nulls in nested data



agronholm
20 Mar 2013, 6:55 AM
I'm having trouble understanding how associations are supposed to work (in ExtJS 4.1.1a).
Consider the following model:


Ext.define('FM.model.Project', {
extend: 'Ext.data.Model',
requires: ['FM.model.Company'],

fields: [
'id',
'customer_id',
'parent_id',
'name',
'code',
'supervisor_id',
'start_date',
'end_date',
'budgeted_hours',
'budgeted_sales',
'budgeted_working_cost',
'budgeted_material_cost',
'info'
],

associations: [
{type: 'belongsTo', model: 'FM.model.Company', associatedName: 'Customer'},
{type: 'belongsTo', model: 'FM.model.Project', associatedName: 'Parent'},
]
});

Sometimes the "parent" or "customer" in the JSON data is null, in which case the association getter should return null as well. But instead, I get an exception:


Con is not a constructor

from ModelManager.js in create(). This happens because the BelongsTo association is trying to create a new instance with the associatedName (which is either "Parent" or "Customer"). This make zero sense to me. Why can't it simply put null in the instanceName? And even if not, why doesn't it use the model to create a new instance? Am I doing something wrong?

mitchellsimoens
22 Mar 2013, 11:00 AM
If you return an empty object does that help? (not saying that it shouldn't be made to accept null)

agronholm
22 Mar 2013, 11:26 AM
If you return an empty object does that help? (not saying that it shouldn't be made to accept null)

Yes, that's how I worked around it. I wonder how everyone else handles this.