【问题标题】:The 'options' parameter in kendo datasourcekendo 数据源中的 'options' 参数
【发布时间】:2013-04-10 09:01:39
【问题描述】:

现在我正在学习使用 kendoui 开发 Web 应用程序,当我尝试使用自定义弹出 kendoWindow 而不是 kendo 内置编辑窗口更新网格数据时,我不知道如何将请求发送到远程服务,所以我尝试在this page的官方api文档中找到答案,但是出现了一个新问题,显示如下代码:

<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>

这是一个示例,用于说明如何将 update 指定为向远程服务发出 HTTP 请求的函数

我的问题是传递给读取和更新函数的参数“选项”是什么。 我找到的唯一线索是transport.parametermap函数的参数,但我不确定它们之间是否有一定的关系,所以希望有人为我解释一下

【问题讨论】:

    标签: kendo-ui datasource


    【解决方案1】:

    options 参数是您已经发现的。 KendoUI 让您指定一个函数,而不是对其数据源类的数据访问方法进行一些配置。

    如果你指定一个函数,kendoUI 怎么会知道你何时完成了数据的加载呢?它不能。 所以有这个选项变量传递给你的函数(实际上,它可以有每个名称,例如 dfhajkfhd),你可以调用它让 kendoUI 知道你的进度。 为此,它具有您可以调用的 successerror 方法。

    您从 kendo-docs 中复制的 cmets 正是这样说的。

    或者你有什么不同的问题?

    【讨论】:

    • 非常感谢您的回答。是的,我从我链接的文档中找到了答案。
    【解决方案2】:

    非常感谢那些回答了这个问题并尝试过的人

    我从问题中链接的 kendo-docs 底部找到了答案。

    1. 创建了一个新的“数据源”对象并定义了读取 并更新其中的传输方式
    2. 当数据源准备好时,使用 dataSource 的“get”方法来获取我需要更新的数据项 按身份证。
    3. 更新数据使用'set'方法并通过'sync'方法提交

    最后一步是将数据同步到数据库 以下是我使用的代码

    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 
    });
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 1970-01-01
      • 2013-09-26
      • 2013-08-08
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多