expand with optional entity

3 posts, 0 answers
  1. Bill
    Bill avatar
    31 posts
    Member since:
    Mar 2013

    Posted 24 Feb Link to this post

    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. Bill
    Bill avatar
    31 posts
    Member since:
    Mar 2013

    Posted 24 Feb in reply to Bill Link to this post

    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" },

  3. Kendo UI is VS 2017 Ready
  4. Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 29 Feb Link to this post

    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!
     
Back to Top