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

Change data for related data on update?

1 Answer 194 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 2
Daniel asked on 25 Dec 2012, 06:49 PM
I am using the $expand to get related data which works fine but I need to change the data that is sent back
to the server when the data is updated

Example if my Server Side Data Model contains two entities
Contact
   ID: number
   firstName: string
   middleName: string
   lastname: string
   ContactType:  ContactType  n-1

ContactType
   ID: nubmer
   name: string
    ContactCollection: ContactType 1-n

Here is my datasource code
function GetContactDS(){
        var MyModel = kendo.data.Model.define({
                                id: "ID",
                                fields: {
                                __KEY: { type: "string" },
                                __STAMP: { type: "number" },
                                ID: { editable: false, nullable: true },                                                               
                                firstName: { type: "string" },
                                middleName: { type: "string" },                                                        
                                lastName: { type: "string" }                               
                                },                             
                            });
                                 
            var crudServiceBaseUrl = "http://127.0.0.1:8081/cors/Contact";
            var  MyDataSource = new kendo.data.DataSource({
        transport: {
       read: function(options) {
               $.ajax( {
                url: crudServiceBaseUrl + '/?$expand=ContactType',
                dataType: "json",
                data: options.data,
                success: function(result) {
                    options.success(result);
                }
            });
        },
                           
    update: function(options) {
            $.ajax( {
                url: crudServiceBaseUrl + "/?$method=update",
                type: "POST",
                dataType: "json",
                data: kendo.stringify(options.data.models),
                success: function(result) {
                    // notify the DataSource that the operation is complete
 
                    options.success(result);
                }
            });
        },
                                destroy: {
                                url: crudServiceBaseUrl + "/?$method=delete",
                                type: "GET"
                                },
                                create: {
                                url: crudServiceBaseUrl + "/?$method=update",
                                dataType: "json",
                                type: "POST"
                                },
                                          parameterMap: function(options, operation) {
                                    if (operation !== "read" && options.models) {
                                       return  JSON.stringify({"__ENTITIES": options.models});
                                    }
                                }
                                },
                                batch: true,
                                pageSize: 30,
                                schema: {
                                     model: MyModel,
                                    data: "__ENTITIES"                         
                                    }
                                 
            });
 
      return MyDataSource;
}
The read request returns this data
{"__entityModel":"Contact","__COUNT":1,"__SENT":1,"__FIRST":0,"__ENTITIES":[{"__KEY":"7","__STAMP":9,"ID":7,"firstName":"jay","middleName":"a","lastName":"blue","ContactType":{"__KEY":"2","__STAMP":4,"ID":2,"name":"Home","contactCollection":{"__deferred":{"uri":"/rest/ContactType(2)/contactCollection?$expand=contactCollection"}}}}]}
Here is the code calling read and binding to grid
var ContactDS = GetContactDS();
 
    $("#grid").kendoGrid({
        selectable: "row",
        filterable: true,
        pageable: true,
        sortable: true,
        change: function(){
        
                 datamodel = this.dataItem(this.select());
                 ID = datamodel.ID
 
        },
        dataSource: ContactDS,
        columns: [
            { field: "ID" },
            { field: "firstName" },
            { field: "middleName" },
            { field: "lastName" },
            {field: "ContactType.name"}
     
        ]
    });


Which works fine I am getting the expanded info for ContactType in my datasource and it binds to a grid fine.
Now I want to update the after it the selected data row is read into a form, reading the data into the form works fine.

The problem is sending the update back to the server which expects a slightly different format for the related entity ContactType
It only needs the changed value of "__Key" to update
Here is my update function:
$("#update").click(function () {
                  datamodel.set("firstName", $("#firstName").val());
                  datamodel.set("lastName", $("#lastName").val());
                  datamodel.set("middleName", $("#middleName").val());
               //   datamodel.set("ContactType.__KEY",3);
             
                   
                   
                  ContactDS.sync();

Here is the data that the server expects
{ "__ENTITIES": [{"__KEY":"7","__STAMP":14,"firstName":"jay","middleName":"a","lastName":"red","ContactType":{"__KEY":"2"}}]}
Here is what kendo.datasource is sending
[{"__KEY":"7","__STAMP":12,"ID":7,"firstName":"jay","middleName":"a","lastName":"blue","ContactType":{"__KEY":"3","__STAMP":2,"ID":3,"name":"Work","contactCollection":{"__deferred":{"uri":"/rest/ContactType(3)/contactCollection?$expand=contactCollection"}}}}]
So how do I either reformat the data or define my model or datasource options to make sure that the extra ContactType fields are removed just leaving the updated "__KEY:" as well as wrapping the whole request in { "__ENTITIES":}

Thanks  for any help!

Dan


1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 26 Dec 2012, 06:42 AM
Hello Dan,

Our understanding is that you are using the GPL version of Kendo UI and intend to continue doing so. In this case, please post your inquiries on the Kendo UI StackOverflow forum:

http://stackoverflow.com/questions/tagged/kendo-ui

Greetings,
Dimo
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
Daniel
Top achievements
Rank 2
Answers by
Dimo
Telerik team
Share this question
or