Datasource create sends incorrect parameter to rails server

3 posts, 1 answers
  1. Fajar
    Fajar avatar
    6 posts
    Member since:
    Dec 2011

    Posted 10 Mar 2012 Link to this post

    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. Answer
    Petyo
    Admin
    Petyo avatar
    2444 posts

    Posted 14 Mar 2012 Link to this post

    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!
  3. Oscar
    Oscar avatar
    2 posts
    Member since:
    Mar 2012

    Posted 20 Mar 2013 Link to this post

    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.



Back to Top