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

Datasource create sends incorrect parameter to rails server

2 Answers 121 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Fajar
Top achievements
Rank 1
Fajar asked on 10 Mar 2012, 12:26 PM
Hi there,

I am struggling with datasource create transport. While normally the correct parameter to a rails server for creating a new item is as follow: 

Started POST "/shoes" for 127.0.0.1 at 2012-03-10 18:10:32 +0700
Processing by ShoesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QrQCjdrZTYpNCbCDeh8iCxSbUdFo7aTHg+GeGKTpNuA=", "shoe"=>{"name"=>"the correct format", "price"=>"", "url"=>"", "replacement_mileage"=>""}, "commit"=>"Create Shoe"}

But kendoui's data source create transport sends the following format:

Started POST "/shoes.json" for 127.0.0.1 at 2012-03-10 18:10:35 +0700
Processing by ShoesController#create as JSON
  Parameters: {"name"=>"shoename"}

Thank you in advance.

My script is as follow:
Item = kendo.data.Model.define({id: "id"});
dataSource =
new kendo.data.DataSource({
     transport: {
        read: {
url: (
"shoes.json")
        },
create: {
url: "shoes.json",
type: "POST"
}
     },
schema: {
model: Item
},

});
dataSource.read();
$(
"#items").html(kendo.render(template, dataSource.view()));
dataSource.add({
"name": "shoename"});
dataSource.sync();

2 Answers, 1 is accepted

Sort by
0
Accepted
Petyo
Telerik team
answered on 14 Mar 2012, 02:42 PM
Hi,

You can use the parameterMap DataSource configuration option to transform the object to the desired data structure. 

Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Oscar
Top achievements
Rank 1
answered on 20 Mar 2013, 05:45 AM
I'm not sure is it the best way to do, however I can get it work.
I really don't like the document of KendoUI, sometime it is so difficult to read, and there isn't enough information for you to learn how to use it.

dataSource = new kendo.data.DataSource({
  transport: {
    read: {
        url: ("shoes.json")
    },
    create: {
        url: "shoes.json",
        type: "POST"
    },
    parameterMap: function(data, type) {
     if (type !== "read" && data) {
       // the demo code is using kendo.stringify(data), however I cant get it work.
       r_data = {shoe: data};
       // for create, will POST {shoe: {name: "new", price: "2", id:""}
       if (type === "update") {
        r_data = {shoe: { name: data.name, price: data.price, url: data.url, replacement_mileage: data.replacement_mileage, id: data.id} };
        // for update, will PUT {shoe: {name:"ABC", price:"1", url:"google.com", replacement_mileage:"1", id:"1"}}
       }
       return r_data;
     }
     return data
    }
  },
    schema: {
        model: Item
    },
});


Here is the example code for the parameterMap that can be used with rails.
And for update part, there will be mass assignment problem(created_at/updated_at/etc.), so I specified the data that PUT to server.



Tags
Data Source
Asked by
Fajar
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Oscar
Top achievements
Rank 1
Share this question
or