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

[Solved] Transport methods and ids created on the server

3 Answers 101 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 2
Iron
Jack asked on 27 Nov 2014, 06:26 PM
My model looks like follows:

var Sample = kendo.data.Model.define({
    id: 'id',
    fields: {
        id: {
            type: 'string',
            nullable: true,
            editable: false
        },
        ....
    }
});

My dataSource uses this model and implements transport methods as functions because I need header and other optios on $.ajax calls. I only represent the create function below:

...
transport: {
    create: function(options) {
        $.ajax(...).done(options.success).fail(options.error);
    }
},
...

My ajax call returns an updated object with an id created on the server, a created date, an updated date and more.

My issue is the object added to the datasource with a null id and sent to the server for creation is not updated with the data I receive in response from the server.

After some digging into the kendo source code, it seems that an update is attempted by searching the object with the same id, but in my scenario teh id changes because it is created on the server. In this case, the object is not updated.

Can you please validate the following workaround or suggest any better ways:

...
transport: {
    create: function(options) {
        var that = this;
        $.ajax(...)
            .done(function(response) {
                that.get(null).accept(response); //<--------------------- this actually updates the item with id === null
                options.success(response);
            )
            .fail(options.error);
    }
},
...









3 Answers, 1 is accepted

Sort by
0
Jack
Top achievements
Rank 2
Iron
answered on 28 Nov 2014, 11:33 AM
It took me quite a while to figure the exact problem out:
  • my read transport which has server paging, sorting and filtering returns a response in the form: { total: x, data: [item1, item2, ..., itemN] }
  • accordingly, I have configured my datasource with:

schema: {
    data: 'data',
    total: 'total'
    ...
}

  • my create transport returns the newly created item
  • The issue is schema.data not only applies to read transports but also to create, update and destroy transports. And for these transports, response.data is undefined.

No need for that.get(null).accept(response), the solution is:

schema: {
    data: function(response) {
        if (response && response.total && response.data) { //read list
            return response.data;
        } else { //create, update, delete
            return response;
        }
    },
    total: 'total',
    ...
}

 



0
Jack
Top achievements
Rank 2
Iron
answered on 28 Nov 2014, 11:35 AM

The documentation should be updated to clearly state:

  1. schema.total only applies to read transports
  2. schema.data applies to all transports
0
Petur Subev
Telerik team
answered on 01 Dec 2014, 03:55 PM
Hello Jack,

Both total and data should follow the schema structure that was defined and they do for all transport operations. The difference is that total is not taken into account for operations different than read.

In other words you cannot return total from the server in the specified field for the Create/Update/Delete operation because the value is self maintainable for those operations.

Our apologies for any confusion.

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