11

我在 Kendo UI 数据源中遇到了一个相当烦人的错误(?)。

当我传递一个自定义函数时,我的传输上的 Update 方法不会被调用,但如果我只给它 URL,它就可以工作。

这有效:

...
transport: {
   update: { url: "/My/Action" }
}
...

这不

...
transport: {
   update: function(options) {
      var params = JSON.stringify({
            pageId: pageId,
            pageItem: options.data
      });
      alert("Update");
      $.ajax({
            url: "/My/Action",
            data:params,
            success:function(result) {
                options.success($.isArray(result) ? result : [result]);
            }
      });
   }
}
...

该函数没有被调用,但是向当前页面 URL 发出了一个 ajax 请求,并且正在发布模型数据,这很奇怪。对我来说听起来像是一个错误。

我需要这个的唯一原因是因为 Kendo 无法弄清楚我的更新操作只返回一个元素,而不是一个数组 - 所以,因为我不想为了满足 Kendo 而弯曲我的 API,我虽然我会反过来做。

有没有人经历过这种情况,并且可以指出我正确的方向?

我也尝试使用 schema.parse,但是在调用 Update 方法时没有调用它。

myDs.sync()用来同步我的数据源。

4

1 回答 1

17

使用文档中的演示按预期工作:

var dataSource = new kendo.data.DataSource({
    transport: {
      read: function(options) {
        $.ajax( {
          url: "http://demos.kendoui.com/service/products",
          dataType: "jsonp",
          success: function(result) {
            options.success(result);
          }
        });

      },
      update: function(options) {
        alert(1);
        // 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", product.UnitPrice + 1);        
    dataSource.sync();
  });

这是一个现场演示:http: //jsbin.com/omomes/1/edit

于 2013-05-14T13:32:10.840 回答