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

XML CRUD

0 Answers 54 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 18 Jul 2012, 05:10 PM
I was having trouble finding a dataSource example for CRUD against a REST based xml service.  I found pieces of the following solution in various posts. Some of this is obvious, but some is not, especially to someone new to kendo and/or jquery.  In case anyone else is looking for something similar, I thought I would post the following example.  This configures a datasource to create/read from an xml REST service using GET for read and PUT for create with an xml document as the payload.  If anyone else has a better solution than the parameterMap to send an xml doc in the payload, please let me know.  Hope this saves someone a headache :)

ds = new kendo.data.dataSource({
transport:{
read: {
url: "/apis/items"
},
create: {
url: "/apis/items/create",
// in another thread, a kendo admin posted that these transport
// apis map to $.ajax, so we can directly add most jquery.ajax options here
type: "PUT",
dataType: "xml",
contentType: "text/xml",
// Must set processData to false if you want to send
// raw data in the request body, see the jquery documentation for more info
processData: false,
},
parameterMap: function (data, type){
// You can pre-process data by mapping a function
// with these parameters to this property.  The first param is in
// the kendo online documentation, the second is not.

if(type === "create"){
return '<item><name>' + data.name + '</name></item>';
}
}
},
schema: {
type: "xml",
data: "/items/item",
model: {
id: "id",
fields: {
id: "@id",
name: "name/text()"
}
}
},
pageSize:10
});

// Now you can add by passing a model w/o the id property to dataSource.add()
ds.add({name: "test"});

// Calling ds.sync() will issue the server request we defined in transport.create to the server.
// the payload will contain <item><name>test</name></item>
ds.sync();

// Hope this helps.  

No answers yet. Maybe you can help?

Tags
Data Source
Asked by
Jonathan
Top achievements
Rank 1
Share this question
or