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

create not being called

5 Answers 42 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
richard
Top achievements
Rank 1
richard asked on 09 Mar 2014, 07:49 PM
ok, using fiddler and chrome tools I don't see a POST go out to my web service for the following when using "people.add"

var people = new kendo.data.DataSource({
      transport: {
        read: {
            type: 'GET', //url being set dynamically and works fine for the read
            dataType: 'json'
        },
        create: {
          url: uri,     //http://wwww.mysite.com/api/people
          type: 'POST',
          dataType: 'json'
        }
      }      
    });
 
//the call:
// data structure on web api POST has same fields as below
//the "add" is working locally but a POST is never made back to the remote web api
// i have tried with and without the JSON.stringify
 people.add(JSON.stringify({FirstName: $("#bfname").val(), LastName: $("#blname").val(), Phone: $("#bphone").val(),
  Email: $("#bemail").val(), ShowId: showID, HostId: 1, DealerId: 1, Note:$("#bemail").val(),  Photo: '', Id: -1}));

i have read posts for hours and hours...tried almost everything i read and can't get the create to POST to my web service.

Thanks for any help.
Richard

5 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 10 Mar 2014, 07:05 AM
Hello Richard,

The add method will only add new item in the DataSource while sync method is which will issue request for sending that item to server.

Regards,
Nikolay Rusev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
richard
Top achievements
Rank 1
answered on 10 Mar 2014, 10:45 AM
I am calling sync also right after the add.
0
richard
Top achievements
Rank 1
answered on 10 Mar 2014, 12:45 PM
here is a look at the full code. I have even tried adding the parameterMap to see if it made a difference:
var people = new kendo.data.DataSource({
      transport: {
        read: {
            type: 'GET',
            dataType: 'json'
        },
        create: {
          url: uri,     //http://wwww.mysite.com/api/people
          type: 'POST',
          dataType: 'jsonp',
          contentType: 'application/json; charset=utf-8'
        },
        parameterMap: function(data, type) {
          console.log('parameterMap called');
      if (type == "create") {
        // send the created data items as the "models" service parameter encoded in JSON
        return { models: kendo.stringify(data.models) };
      }
    }
      },
      schema : {
        model:{
          id: "Id",
          fields: {
            Id: {editable: false, type: "number"},
            DealerId: {type: "number"},
            ShowId: {type: "number"},
            HostId: {type: "number"},
            FirstName: {validation: {required: true}},
            LastName: {validation: {required: true}},
            Phone: {validation: {required: true}},
            Email: {validation: {required: true}},
            Photo: {validation: {required: false}},
            Note: {validation: {required: false}}
          }
        }
      }    
    });
 
function savePeople() {
 
  people.add({FirstName: $("#bfname").val(), LastName: $("#blname").val(), Phone: $("#bphone").val(),
  Email: $("#bemail").val(), ShowId: showID, HostId: 1, DealerId: 1, Note:$("#bemail").val(),  Photo: '', Id: -1});
 
  people.sync();
}
0
Andrew
Top achievements
Rank 1
answered on 10 Mar 2014, 01:09 PM
It might be caused by the non-empty value for the "Id" property. The DataSource might think that the object is not "new" because it has a value for the "Id". Try removing the Id property from the object, or in the schema, set the defaultValue for the Id to "-1".
0
Nikolay Rusev
Telerik team
answered on 10 Mar 2014, 03:24 PM
Hello Richard,

Indeed this is correct. The `Id` field is of type `number` and default value for numbers is `0`. However you are creating models with `Id` field initialized to `-1` and the model is not considered new.

You should either define defaultValue for the `Id` field in the model definition or skip setting `Id` field when creating new models.

Regards,
Nikolay Rusev
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
richard
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
richard
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Share this question
or