Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'options' parameter in kendo datasource

Now I am learning to developing a web app with kendoui, when I try to udpate the grid data with cusomized popup kendoWindow instead of the kendo built-in edit window, I didn't know how to send the request to the remote serve, so i try to find the answer in official api docs in this page, but there is a new problem occured, show as the follow code:

<script>
    var dataSource = new kendo.data.DataSource({
        transport: {
            read  : function (options) {
                /* implementation omitted for brevity */
            },
            update: function (options) {
                // make JSONP request to http://demos.kendoui.com/service/products/update

                $.ajax({
                    url     : "http://demos.kendoui.com/service/products/update",
                    dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
                    // send the updated data items as the "models" service parameter encoded in JSON
                    data    : {
                        models: kendo.stringify(options.data.models)
                    },
                    success : function (result) {
                        // notify the data source that the request succeeded

                        options.success(result);
                    },
                    error   : function (result) {
                        // notify the data source that the request failed
                        options.error(result);
                    }
                });
            }
        },
        batch    : true,
        schema   : {
            model: { id: "ProductID" }
        }
    });

    dataSource.fetch(function () {
        var product = dataSource.at(0);
        product.set("UnitPrice", 20);
        dataSource.sync(); makes request to http://demos.kendoui.com/service/products/update
    });
</script>

it's a example for illustrate how to specify update as a function to make a HTTP request to the remote service

my problem is what is the parameter 'options' is that passed to read and update function. the only clue I've found is the parameters for transport.parametermap function, but I am not sure there are certain relationship between them, so hope someone some explain for me

like image 446
成 周 Avatar asked Apr 17 '13 14:04

成 周


People also ask

What is DataSource in kendo?

The Kendo UI DataSource component plays a central role in all web applications built with Kendo UI for jQuery. The DataSource is an abstraction for using local data (arrays of JavaScript objects) or remote data (web services returning JSON, JSONP, oData, or XML).

What is schema in kendo grid?

schema ObjectThe configuration used to parse the remote service response.

How do I set data for Kendo grid?

Bind data to Kendo Grid by using AJAX Read action method. Change the datasource on change event of any HTML controls. Normally, a developer can bind the data to Grid by using AJAX Read method. This read method is ActionResult method in MVC which will return JSON DataSourceResult & direclty bind the data to Grid.


2 Answers

The options parameter is what you have already discovered. KendoUI let's you specify a function instead of some configuration for the data-access methods of it's datasource class.

If you specify a function, how could kendoUI ever know when you are finished with loading the data? It couldn't. So there is this options-variable that is passed to your function (and really, it could have every name, for example dfhajkfhd) which you can call to let kendoUI know about your progress. For this it has the success and error methods which you can call.

Your comments, which you have copied from the kendo-docs, say exactly this.

Or was you question something different?

like image 188
Shion Avatar answered Nov 09 '22 07:11

Shion


Really appreciate the ones who have answered this question and try to

I've found the answer from bottom of the kendo-docs that i linked in the question.

  1. created a new 'dataSource' object and define the read and update transport in it
  2. when dataSource ready , use the 'get' method of dataSource to get the data item i need to udpate by id.
  3. udpate data use 'set' method and submit by 'sync' method

the last step is sync the data to database follow is the code i used

var updateDataSource = new kendo.data.DataSource({
    type: "odata",
    transport: {
        read: {
            url: "/api/odata/PEMEP/TaskInformations/?"
        },
        update: {
            url: "/api/odata/PEMEP/TaskInformations/?",
            type: "PUT",
            dataType: "json",
        },
    },
    schema: {
        model: {
            id: "_id"
        }
    },
    sync: function() { // close edit window when update request finished

        $("#window").data("kendoWindow").close();
    },
    error: function(e) {
        console.log(e.status);
    }
});
updateDataSource.fetch(function() {

    var task = updateDataSource.get(id); // get dataitem by id

    task.set("status", status); // set new values
    task.set("retreatReason", retreatReason);

    updateDataSource.sync(); //submit the change 
});
like image 22
成 周 Avatar answered Nov 09 '22 08:11

成 周