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

DataSource does not call create

5 Answers 13 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 23 Dec 2013, 09:49 AM
Hi all!

I am very beginner in Kendo, so sorry if I ask stupid questions :)
I have the following code:

    var template = kendo.template($("#todo-template").html());

    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "api/todos",
                dataType: "json"
            },
            create: function () {
                alert("DSA");
            }
        },
        schema: {
            model: { id: "TODOID" }
        },
        change: function () { // subscribe to the CHANGE event of the data source
            $("#todo-table tbody").html(kendo.render(template, this.view())); // populate the table
        }
    });
    dataSource.read();

    $("#add-button").click(function () {
        dataSource.add({
            "TODOID": 30,
            "TITLE": "asd",
            "PRIORITY": 3,
            "DEADLINE": "2013-02-02",
            "DONE": false
        });

        dataSource.sync();
    });

The problem is that the create event is never being called. The read is okay, the table is shown nicely. What would be the problem?

Sincerely,
Daniel

5 Answers, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 23 Dec 2013, 09:55 AM
Hi Daniel,

Basically the transport options should all be of the same type. For example if the read option is set as an object then the create, update and delete should also be objects instead of functions.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Daniel
Top achievements
Rank 1
answered on 23 Dec 2013, 10:01 AM
Hi Alexander,

Thanks for the quick answer.
This alert function was the my last hope. The original version is this:
...
transport: {
            read: {
                url: "api/todos",
                dataType: "json"
            },
            create: {
                url: "api/todos",
                dataType: "json",
                type: "POST"
            },
        },
        schema: {
            model: { id: "TODOID" }
        },
...

But the create event is still not being called and I dont have any idea why.
0
Alexander Popov
Telerik team
answered on 23 Dec 2013, 10:32 AM
Hello Daniel,

I reviewed the attached code again and noticed that you are adding an item that has its ID field populated. Basically the dataSource differentiates new items from existing (create or update) based on whether or not they have an ID. I would recommend you to add the items without an ID and set it on the back-end, where you have direct access to the data base.

Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Daniel
Top achievements
Rank 1
answered on 23 Dec 2013, 11:06 AM
Hi,

If I dont include the ID to the new item, I get this error message:
"ReferenceError: TODOID is not defined"

Edit: I dont know if it counts, but the webapi I want to reach is an open access web api controller auto generated from a domain model.

Daniel
0
Accepted
Daniel
Top achievements
Rank 1
answered on 23 Dec 2013, 11:55 AM
Okay, I finally figured it out. In the template I used the TODOID field, so this is why the error message came up.
Tags
General Discussions
Asked by
Daniel
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or