This is a migrated thread and some comments may be shown as answers.

expand with optional entity

2 Answers 101 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 2
Bill asked on 24 Feb 2016, 09:12 PM

I'm trying to figure out how to configure an odata v3 dataSource for a field that belongs to an optional entity, particularity when using the dataSource to bind to a grid

For example, I have TableA which has an optional foreign key (FK) to TableB.  My dataSource: transport: read has a url property set to odata/TableA and mydata object has $expand: "TableB".  All this works great as long as every row in TableA has a valid TableB_Id.  However, TableB_id is nullable, and when a row exists that has null for TableB_id, my application breaks.  The error appears because in my model, I have defined a filed as follows:     Name: { type: "string", nullable: true, from: "TableB.Name" },

The "from" specifies the expand entity but when the row happens to have a nullable TableB_id, TableB is undefined for that row, causing the app to crash.  Placing nullable:true in the field definition probably only addresses the last segment of whats in the from statement.  

Is there an easy way to address this? In my real world app, I have 5 optional tables, each with fields I'd like to bring to the grid with default type values acceptable if the entity doesn't exist.

Kendo UI v2016.1.112

2 Answers, 1 is accepted

Sort by
0
Bill
Top achievements
Rank 2
answered on 24 Feb 2016, 11:33 PM

I've solved my issue, but I'm not sure it's the best approach.  It does allow me to keep this optional relationship though and filtering and grouping appear to be working well.  Basically, all I had to do was parse the response to check for an undefined entity and if so replace it with an empty object.  Also, set nullable: true on the field definition in the schema model.

parse: function(response) {

   for (var i = 0; i < response.value.length; i++) {         

      //for each optional entity in your OData expand, add an entry here         

      if (response.value[i]['TableB'] == undefined) { response.value[i]['TableB'] = {} }        

   }     

   return response;

}
//field defintion in schema model
TableB_SomeNumber: { type: "number", nullable: true, from: "TableB.NumberOfSomething" },

0
Rosen
Telerik team
answered on 29 Feb 2016, 09:34 AM

Hello Bill,

Indeed, you should use the schema.parse function to normalize the fields when a nested expression paths specified and  which could have a null object in some part. 

The schema.model.fields.nullable option is applicable only for creating new records and it determines if the value for the field should be read from the defaultValue option.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Bill
Top achievements
Rank 2
Answers by
Bill
Top achievements
Rank 2
Rosen
Telerik team
Share this question
or