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

DataSource transport with function

3 Answers 1248 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
amp
Top achievements
Rank 1
amp asked on 16 Sep 2013, 02:57 PM
Hi,

According to DataSource Transport API, I am using the following code for my transport:

01.var historyTransport = {
02.    read: function (options) {
03.        $.ajax({
04.            url: "../../AppData/Batch/History.json",
05.            dataType: "json",
06.            success: function (response) {
07.                // notify the data source that the request succeeded
08.                console.log('successfully load History.json');
09.                var rows = [];
10.                $.map(response.rows, function (n,i) {
11.                    var obj = { status: n.cell[0] };
12.                    rows.push(obj);
13. 
14.                });
15.                var historyData = {};
16.                historyData.rows = rows;
17.                options.success(historyData);
18. 
19.            },
20.            error: function (response) {
21.                // notify the data source that the request failed
22.                console.log('failed to load History.json');
23.            }
24.        });
25.    }
26.}
I am having a very strange problem with this options.success function. my json object from the ajax response looks like this
Object {rows: Array[7675], page: 1, total: 1, records: "1"}
options.success() has no problem to parse it. However, if i want to do some reformatting by creating a separate object historyData, i will have an error from options.success() saying TypeError: Cannot call method 'slice' of undefined, unless i name the array  property of historyData - rows.

it almost feels like the DataSource options.success() function is expecting an array called rows. it was pure coincident that my original json contains this rows property.

can anyone help to explain whether my finding is true? and what exactly is this options parameter? there is no document about it.

Thanks
amp








3 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 16 Sep 2013, 04:31 PM
Hello,

The DataSource is expecting array with objects. The field name from the server response which contains that array is configured through schema.data property.

The options parameter contains request data (if there is any) such as page, pageSize, filter parameters and etc. It also gives access to two methods - success and error. The first one is used to pass the raw data to the DataSource parser for further processing. The second one is used to indicate failure and will trigger error event of the DataSource. Any error messages should be passed through it.

Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
amp
Top achievements
Rank 1
answered on 16 Sep 2013, 08:59 PM
Hi Alexander,

thanks for your reply. as matter of fact i do have a schema configured for the datasource but i dont really understand its role and when exactly is the schema is applied during the data source operation. my schema looks like 
1.var historySchema =  {
2.    data: function(response) {
3.        return response.rows; // twitter's response is { "results": [ /* results */ ] }
4.    }
5.};
1. so it is because i have this schema configured Datasource is expecting the Array object to be called 'row'?
 - I have tested this one out. the reason for DataSource to look for 'rows' is indeed because I have set the schema data. I am still not able to get my head around why this Data is required. when options.success() function is called why cant the data be set directly there?

2. can you please give me a lowdown on the sequence of various configurations of datasource being called? i tried to put a breakpoint into the schema function i created. but it never stoppped the execution.

3. my json response doesnt meet my ui requirement exactly because it is shared by different applications. i only need part of the info from the rows Array, therefore i need to create an new Array object based on the response Ajax has returned. it is like a computed subset of rows Array. where is the correct place to perform this reformatting?
 - after reading the api for datasource schema again, it looks like the 'parse' configuration is the right place for this kind of pre-propcess. However this 'parse' will only be applied if the ajax call is successful right? if there is any ajax call failure and i want to create the {error:'some error message'} for the schema.error i will still have to do this in the ajax call, is that correct?

4. the datasource will be used to populate a grid. i have been reading about the filter. The grid's datasource will refresh every 3 mins by a timer, my user often needs to  to place a filter on the grid columns to monitor certain type records. where is the best place to implant this filter logic, therefore when grid refreshes the filter will automatically applied (rather than the grid flashes once to display all the record, then flashes the second time because the filter is re-applied)

5. Will the performance of filtering be impacted if there is no id field specified in the schema.model?

Thank you very much. sorry to pop 4 questions in a row. i have been long battling them.

regards
yiwei

0
Alexander Valchev
Telerik team
answered on 18 Sep 2013, 01:50 PM
Hello,

1. So it is because i have this schema configured Datasource is expecting the Array object to be called 'row'?

 - I have tested this one out. the reason for DataSource to look for 'rows' is indeed because I have set the schema data. I am still not able to get my head around why this Data is required. when options.success() function is called why cant the data be set directly there?

options.success passes the raw response to the DataSource for further processing. As you know the Ajax request is asynchronous, the main purpose of success function is to indicate that the request has finished successfully, response is received and the DataSource can start parsing it.

There is no way for the framework to know the format of the server response. It could be basically anything:
//response
{ "foo": { "bar": { "bar": [<records>] } }, "total": 123 }
 
//response
[<records>]
 
//response
{ "anything": [<records>], "total": 123 }

Which is why you should specify where the [<records>] array is located. This is why schema.data is used for. You can omit it only if you are directly passing the [<records>] array like this.
var response = [<records>];
options.success(response);

2. Can you please give me a lowdown on the sequence of various configurations of datasource being called? i tried to put a breakpoint into the schema function i created. but it never stoppped the execution.

I believe that #1 gives an answer to this question. Anyway, the schema.data for each case should look like:
//case 1
data: function(response) {
    return response.foo.bar.baz;
}
 
//case 2
//can be omitted
data: function(response) {
    return response;
}
 
//case 3
data: function(response) {
    return response.anything;
}


3. my json response doesnt meet my ui requirement exactly because it is shared by different applications. i only need part of the info from the rows Array, therefore i need to create an new Array object based on the response Ajax has returned. it is like a computed subset of rows Array. where is the correct place to perform this reformatting?
 - after reading the api for datasource schema again, it looks like the 'parse' configuration is the right place for this kind of pre-propcess. However this 'parse' will only be applied if the ajax call is successful right? if there is any ajax call failure and i want to create the {error:'some error message'} for the schema.error i will still have to do this in the ajax call, is that correct?

Generally speaking it is best to fix the way data is processed on the server and to return only the needed information ready for use.
The parse function is not supposed to be executed only if the Ajax request is successful. If the request fails there will be no data for parsing, right?

5. Will the performance of filtering be impacted if there is no id field specified in the schema.model?

No. Other features however, such as editing, may not work as expected.

Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
amp
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
amp
Top achievements
Rank 1
Share this question
or