我在Kendo UI Datasource中经历了一个相当恼人的错误(?).
当我传递一个自定义函数时,我的传输上的我的Update方法没有被调用,但是如果我只给它一个URL就可以了.
这有效:
...
transport: {
update: { url: "/My/Action" }
}
...
Run Code Online (Sandbox Code Playgroud)
事实并非如此
...
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]);
}
});
}
}
...
Run Code Online (Sandbox Code Playgroud)
该函数未被调用,但是对当前页面URL进行了ajax请求,并且正在发布模型数据,这很奇怪.对我来说听起来像个错误.
我需要这个的唯一原因是,因为剑道无法弄清楚我的更新动作只返回一个元素,而不是一个数组 - 所以,因为我不想弯曲我的API只是为了满足剑道,我虽然我会反过来这样做.
有没有人经历过这个,可以指出我正确的方向?
我也尝试使用schema.parse,但是在调用Update方法时没有调用它.
我myDs.sync()用来同步我的数据源.
Ata*_*hev 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();
});
Run Code Online (Sandbox Code Playgroud)
这是一个现场演示:http://jsbin.com/omomes/1/edit