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

Create doesn't get called with an id != 0

2 Answers 152 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Roberto
Top achievements
Rank 1
Roberto asked on 26 Mar 2012, 10:08 AM
I'm noticing some strange behavior on datasource CRUD operation when sync() gets called.
I've a transport with this configurations:

new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "webservices/entities/read",
                        dataType: "jsonp",
                        jsonp: false,
                        jsonpCallback: "callback"
                    },
                    create: {
                        url: "webservices/entities/create",
                        type: "post",
                    },
                },
                schema: {
                    data: "items",
                    total: "total",
                    model: {
                        id: "id",
                        fields: {
                        id: {type: "number"},
                        name: {type: "string"},
                        description: {type: "string"},
                        parent_id: {type: "number"}
                    }
                },
                serverFiltering: true,
                sort: {field: "name", dir:"asc"},
                filter: {field: "parent_id", operator: "eq", value: parent_id}
});

however, when I add a new item with the datasource.add() method and call sync(), Create gets called only if I set the id: 0. With any other number Create doesn't get called. I've track the XHR object from the browser developer tools and I can also verify this by setting parameterMap: function(operation, options) { alert(operation); }

Any clue?  

2 Answers, 1 is accepted

Sort by
0
Roberto
Top achievements
Rank 1
answered on 26 Mar 2012, 01:21 PM
To be more specific, I Need to add a row based on a form compilation.

So I've created a grid and linked to the datasource like this:
grid = $('.grid');
grid.kendoGrid({
dataSource: gridDS,
     selectable: "cell",
     columns: [
         {field: "name", title:"Name"},
         {field: "description", title:"Description"}
     ]
    });

A button binded to click event fire a kendoWindow:
var form = $('#editWindow');
form.kendoWindow({
    modal: true,
    resizable: false,
    visible: false,
    title: "Entities",
    width: 400
});
 
var kWindow = form.data('kendoWindow');
         
$('#add-entity').bind('click', function(){
    kWindow.center();
    kWindow.open();
});

Then, a save button try to add the new data to the datasource:
$('#save').bind('click', function(){
    var entityName = $('input[name$="entity_name"]').val();
    var entityDescription = $('input[name$="entity_description"]').val();
     
        genId = Math.random().toString(10).substr(2,10);
         
    gridDS.add({ 
            id: genId,   // Here I'm trying to generate an uuid, but It could be any number. It Works only with '0' value.
        name: entityName,
        description: entityDescription,
        parent_id: entity_id     // this is set somewhere else...
    });
 
        gridDS.sync()  // Sync doesn't call the DataSource Create method unless id = 0 (?!)                            
    kWindow.close();           
});
0
Iliana Dyankova
Telerik team
answered on 28 Mar 2012, 08:50 PM
Hi Roberto,

This is actually expected behavior. By design, the DataSource needs the ID to be with default value to consider it for a new item.


Greetings,
Iliana Nikolova
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
Roberto
Top achievements
Rank 1
Answers by
Roberto
Top achievements
Rank 1
Iliana Dyankova
Telerik team
Share this question
or