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

DataSource, odata and put request

1 Answer 212 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 26 Jul 2013, 05:09 PM
Hi All,

I have been trying to get the datasource to refresh or update itself with the contents of a PUT response it sends to an odata service.  I know normally the PUT would return 204 with updating  a record, but I am returning a 200 with the contents that where updated.  The main reason for this is that a timestamp (ETAG) value is updated and returned in the response.  So the next time an edit is done on it should work, but currently it does not.

I have checked fiddler request/responses they are correct, it just that data source is not being refreshed with the contents being sent in the response.

Do I have to do a force refresh on the datasource and retrieve that page of data or is there another way?

The timestamp field is nested in the json response so I don't know if that is the cause or not.

Here is an example of the  request

PUT https://localhost:444/admin/v2/accounts(6) HTTP/1.1
Host: localhost:444
Connection: keep-alive
Content-Length: 325
Cache-Control: max-age=0
Accept: application/json, text/javascript, */*; q=0.01
Origin: https://localhost:444
X-Requested-With: XMLHttpRequest
If-Match: "AAAAAAAADJ0="
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
Content-Type: application/json
Referer: https://localhost:444/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

{"Id":"6","Name":"Test Account  5","Registered":null,"WebsiteUrl":"Testing","IsEnabled":true,"AccountType":"Retailer","Tracking":{"CreatedBy":"JhH1TV-f-P8cZgV19W0bhyI3UBLlHdbh79ZkzhnJSKA","CreatedOn":"2013-07-26T14:08:22.787","ModifiedBy":"JhH1TV-f-P8cZgV19W0bhyI3UBLlHdbh79ZkzhnJSKA","ModifiedOn":"2013-07-26T16:54:20.57","Timestamp":"AAAAAAAADJ0="}}

Here is an example of the response.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
ETag: "AAAAAAAADJ4="
Access-Control-Allow-Origin: *
DataServiceVersion: 3.0
Date: Fri, 26 Jul 2013 17:05:14 GMT
Content-Length: 425

{
  "odata.metadata":"https://localhost:446/admin/v2/%24metadata#accounts/@Element","Id":6,"Name":"Test Account  5","Registered":null,"WebsiteUrl":"Testing","IsEnabled":true,"AccountType":"Retailer","Tracking":{
    "CreatedBy":"JhH1TV-f-P8cZgV19W0bhyI3UBLlHdbh79ZkzhnJSKA","CreatedOn":"2013-07-26T14:08:22.787","ModifiedBy":"JhH1TV-f-P8cZgV19W0bhyI3UBLlHdbh79ZkzhnJSKA","ModifiedOn":"2013-07-26T17:05:13.9989111Z","Timestamp":"AAAAAAAADJ4="
  }
}

As you can see the ETag/Timestamp have been updated.

Any suggestions

Regards

Richard....

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 26 Jul 2013, 08:58 PM
Hi All,

Just in case anyone else ran in to this issue here is a hack for now.  Basically I have to manually call Ajax with the update function and then manually update the dataSource if successful.  I have tested this, but assume it can be done differently and better.

Regards

Richard...


update: function(options) {
var url = accountsUrl + "(" + options.data.Id + ")";
var data = JSON.stringify(options.data);
var item = $("#grid").data("kendoGrid").dataSource.get(options.data.Id);
$.ajax({
url: url,
dataType: "json",
type: "PUT",
contentType: "application/json",
data: data,
beforeSend: function (xhr) {
var etag = options.data.Tracking.Timestamp;
if (etag != undefined) {
console.log("Before PUT Timestamp is - " + etag);
xhr.setRequestHeader("If-Match", "\"" + etag + "\"");
}
},
success: function(result) {
console.log("Update Success Etag = " + result.Tracking.Timestamp);
item.set("Tracking.Timestamp", result.Tracking.Timestamp);
item.dirty = false;
options.success(result);
},
error: function(result) {
// notify the data source that the request failed
options.error(result);
}
});
},
Tags
Data Source
Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or