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

remote data binding - json - http 304

5 Answers 83 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Dierik Vermeir
Top achievements
Rank 1
Dierik Vermeir asked on 02 Sep 2013, 09:40 AM
Hey,

I have this use case:

On the server I have a JSON file that changes (for example) every 15 minutes.
The client typically leaves his browser open and wants to see the changes as fast as possible.

On the server I solved it this way:
When getting the json file, the server uses a http 304 to indicate that the file hasn't changed since the last request.


Client side I solved it this way:
 I have a kendo datasource, and an interval that is doing  datasource.read   (polling mechanism):

Problem:
The data isn't changed, but still the "change" event of the datasource is triggered.

Is there any way of achieving this with kendo datasource, without reprogramming the entire datasource with $.ajax?

Or is this maybe a good feature request :-) ?
As an extra: I don't want to use polling, but look at the Expires http header, to know when to do the next request.

My code:
01.var datasource = new k.data.DataSource({
02.    transport: {
03.        read: {
05.                url: 'test.txt',
06.                dataType: 'json'
07.            }
08.        },
09.    schema: {
10.        data: function (data) {
11.            return data.Layers || [];
12.        }
13.    }
14.});
15. 
16.//polling..
17.var _interval = window.setInterval(function () {
18.        datasource.read();
19.}, 5000);
20. 
21.datasource.bind('change', function (e) {
22.    //This shouldn't be triggered every 5 seconds
23.    console.log(this.data().length);
24.});
The response received from the server looks like this:

HTTP/1.1 304 Not Modified
Last-Modified: Mon, 02 Sep 2013 08:02:11 GMT
Expires: Mon, 02 Sep 2013 08:15:00 GMT
Accept-Ranges: bytes
ETag: "383bbbab2a7ce1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 02 Sep 2013 09:23:10 GMT
Kind regards,

5 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 03 Sep 2013, 08:26 AM
Hello,

 The change event of the Kendo UI data source is raised by design every time data is received. This is required in order for Kendo UI widgets to be rebound when data is refreshed. Currently the data source doesn't keep the old data and check against the new response to see if anything has changed. 

 If you want to use the expires HTTP header you would need to create a custom transport for the data source. In that transport you can avoid calling options.success(response) if you wish to suppress the change event. Something like this:

transport: {
   read: function(options) {
         $.ajax({
             url: "test.txt",
             success: function(response, status, xhr) {
                   if (xhr.status !== 304) {
                        options.success(response);
                   }
             }
          });
   }
}

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Andrew
Top achievements
Rank 1
answered on 04 Sep 2013, 02:30 PM
Since the 304 response is the HTTP standard for indicating that content hasn't changed, shouldn't the success handler built into DataSource also check for this? In DataSource.read(), the transport is passed the DataSource.success function. Like the code sample you showed, it would seem that the DataSource should check for a 304 response and not modify the internal data in that case.
0
Atanas Korchev
Telerik team
answered on 04 Sep 2013, 02:52 PM
Hello Andrew, 

This sounds like a nice idea. Do you mind opening a feature request in our feedback portal?

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Accepted
Andrew
Top achievements
Rank 1
answered on 04 Sep 2013, 03:16 PM
0
Dierik Vermeir
Top achievements
Rank 1
answered on 04 Sep 2013, 03:25 PM
Thanks for the answers.

Also thanks for the feature request.  I was going to do this, but you have beaten me in time.   I gave it 3 votes.

I'm going to add another feature request, to automatically  re-read the datasource  on the moment the Expires-http-header indicates.
This is a standard http header that is used very often in cooperation with http 304.

kind regards
Tags
Data Source
Asked by
Dierik Vermeir
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Andrew
Top achievements
Rank 1
Dierik Vermeir
Top achievements
Rank 1
Share this question
or