Aravind
Aravind

Reputation: 11

Kendo-UI DataSource.read() not always making a GET request

I do have a kendo datasource which populates a kendo template.

var remoteTemplate = kendo.template($("#remotetemplate").html(), { 
useWithBlock: false });

var remoteDatasource = new kendo.data.DataSource({    

    transport: {
        read: {
            url: 'Home/RemoteData',                    
        }
    },
    change: function () {
        $("#remotemovies tbody").html(kendo.render(remoteTemplate, this.view()));
    }
});

A GET request is made to Home/RemoteData when we call the read method remoteDatasource.read()

One more read , another request is made to Home/RemoteData I know this is not good , but I am trying to understand this :-) remoteDatasource.read()

So far good , however once more time I call the read() , GET request is not happening. Why is that? remoteDatasource.read()

No Get request here no matter how many times I call after this

Also I noticed the same behaviour with fetch() method.

Can someone explain me why is this behaviour? also what is the difference between read and fetch.

Upvotes: 0

Views: 8916

Answers (3)

Aravind
Aravind

Reputation: 11

It seems to optimize the behavior Kendo is limiting the number of calls to two if you call datasource.read() in successive lines. Which is fair as there is no need in practical scenario to have such a code.

Here are the two types of code i have written.

Scenario 1 : Calling the datasource.read() in response to a button click.

    $("#remoteRequestBtn").click(function () {
        var remoteTemplate = kendo.template($("#remotetemplate").html(), { useWithBlock: false });

        var remoteDatasource = new kendo.data.DataSource({
            transport: {
                read: {
                    cache: false,
                    url: 'Home/RemoteData',
                }
            },
            change: function () {
                $("#remotemovies tbody").html(kendo.render(remoteTemplate, this.view()));
            }
        });

        remoteDatasource.read();

    }); 

Result : A Get request is called to the web api , whenever i click the button.

Scenario 2 : Calling multiple datasource.read() in response to a button click.

$("#remoteRequestBtn").click(function () {
        var remoteTemplate = kendo.template($("#remotetemplate").html(), { useWithBlock: false });

        var remoteDatasource = new kendo.data.DataSource({
            transport: {
                read: {
                    cache: true,
                    url: 'Home/RemoteData',
                }
            },
            change: function () {
                $("#remotemovies tbody").html(kendo.render(remoteTemplate, this.view()));
            }
        });

        remoteDatasource.read();
        remoteDatasource.read();
        remoteDatasource.read();
        remoteDatasource.read();

    });

Note : I am calling the read method 4 times , Now if you ask me if this is a valid scenario :-) It is not :-)

Result : In this case i get only two GET requests (For the first and second read() , Rest of the reads are ignored )

As of now i would like to treat this as an optimization from the Kendo-UI side , unless someone comeback and correct it.

Special Thanks to JFlok & CodingWithSpike for giving right directions.

Upvotes: 1

CodingWithSpike
CodingWithSpike

Reputation: 43718

Does your change callback or any other Javascript throw an exception? Try removing your change handler function. I've seen issues in the past where if an uncaught exception is thrown while the DataSource is trying to process the server response, then the DataSource is left in a state where it thinks the previous request is still running (because it never finished due to the error) and won't do another request.

Upvotes: 0

JFlox
JFlox

Reputation: 708

The read() method is supposed to request the remote service every time.

The fetch() method only requests the remote service the first time.

Your particular read() may not be requesting the remote service because it is caching. Can you try your request as a POST or set the configuration in transport.read.cache to false?

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-transport.read.cache

Upvotes: 2

Related Questions