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

Excluding properties not on model

2 Answers 184 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 08 Apr 2013, 06:59 PM
I retrieved an object via an OData WebAPI service using the DataSource, where the response looks like this:
{"odata.metadata":"http://localhost:49639/api/$metadata#Address/@Element","AddressKey":"1","AddressLine1":"test2","AddressLine2":"Suite 2004","City":"Lawrenceville","State":"Georgia","PostalCode":"30043"}
My schema's model does not have "odata.metadata", and I don't want it included.  The reason this extra property is a problem is that when I do an update, it sends "odata.metadata" as an item back on the OData POST request, and my WebAPI throws an error when trying to automatically map that, so I'd rather it just not send it at all.  Right now, I'm manually deleting the property in the parse function to remove it, but it just feels wrong.  It seems like the DataSource should not create an object with properties from my response that do not exist in the schema's model, so maybe I'm missing something here.  Is the only way to handle this by manually doing something in the parse function?
parse: function(response) {
    // This property causes an issue when sending the model back, so removing it.
    delete response["odata.metadata"];
},







2 Answers, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 1
answered on 09 Apr 2013, 01:55 PM
Here's another version that will loop through the members on the response, and remove any that do not exist in the schema's model.  I'm still interested to hear if I'm doing something wrong with my approach though.
parse: function(response) {
    // Remove any properties on the response that do not map to the model.
    var f = viewModel.dataSource.options.schema.model.fields;
    $.each(response, function (key, value) {
        if (!(key.toString() in f)) {
            delete response[key];
        }
    });
    return response;
},


0
Petur Subev
Telerik team
answered on 10 Apr 2013, 02:58 PM
Hello Michael,

Indeed we are not stripping any additional information (metadata) from the response - whatever is passed from the server is added to the datasource and thus send back to the server when changes are saved.
You are completely on the right track to use the schema.parse function to get rid of that, in fact I would suggest you to do the same. I am not sure if there is actually another more convenient way to handle this scenario.

Basically it is up to the version of the oData that you are using. The format is constantly changing.


Kind Regards,
Petur Subev
the Telerik team
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
Michael
Top achievements
Rank 1
Answers by
Michael
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or