DataSource does not call create

6 posts, 1 answers
  1. Daniel
    Daniel avatar
    5 posts
    Member since:
    Dec 2012

    Posted 23 Dec 2013 Link to this post

    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
  2. Alexander Popov
    Admin
    Alexander Popov avatar
    1444 posts

    Posted 23 Dec 2013 Link to this post

    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!
  3. Daniel
    Daniel avatar
    5 posts
    Member since:
    Dec 2012

    Posted 23 Dec 2013 Link to this post

    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.
  4. Alexander Popov
    Admin
    Alexander Popov avatar
    1444 posts

    Posted 23 Dec 2013 Link to this post

    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!
  5. Daniel
    Daniel avatar
    5 posts
    Member since:
    Dec 2012

    Posted 23 Dec 2013 Link to this post

    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
  6. Answer
    Daniel
    Daniel avatar
    5 posts
    Member since:
    Dec 2012

    Posted 23 Dec 2013 Link to this post

    Okay, I finally figured it out. In the template I used the TODOID field, so this is why the error message came up.
Back to Top