我正在使用 $expand 来获取工作正常的相关数据,但是我需要更改在数据更新时发送回服务器的数据
例如,如果我的服务器端数据模型包含两个实体联系人 ID:号码 firstName:字符串 middleName:字符串姓氏:字符串 ContactType:ContactType n-1
ContactType ID:编号名称:字符串 ContactCollection:ContactType 1-n
这是我的数据源代码
function GetContactDS(){
var MyModel = kendo.data.Model.define({
id: "ID",
fields: {
__KEY: { type: "string" },
__STAMP: { type: "number" },
ID: { editable: false, nullable: true },
firstName: { type: "string" },
middleName: { type: "string" },
lastName: { type: "string" }
},
});
var crudServiceBaseUrl = "http://127.0.0.1:8081/cors/Contact";
var MyDataSource = new kendo.data.DataSource({
transport: {
read: function(options) {
$.ajax( {
url: crudServiceBaseUrl + '/?$expand=ContactType',
dataType: "json",
data: options.data,
success: function(result) {
options.success(result);
}
});
},
update: function(options) {
$.ajax( {
url: crudServiceBaseUrl + "/?$method=update",
type: "POST",
dataType: "json",
data: kendo.stringify(options.data.models),
success: function(result) {
// notify the DataSource that the operation is complete
options.success(result);
}
});
},
destroy: {
url: crudServiceBaseUrl + "/?$method=delete",
type: "GET"
},
create: {
url: crudServiceBaseUrl + "/?$method=update",
dataType: "json",
type: "POST"
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return JSON.stringify({"__ENTITIES": options.models});
}
}
},
batch: true,
pageSize: 30,
schema: {
model: MyModel,
data: "__ENTITIES"
}
});
return MyDataSource;
}
读取请求返回此数据
{"__entityModel":"Contact","__COUNT":1,"__SENT":1,"__FIRST":0,"__ENTITIES":[{"__KEY":"7","__STAMP":9,"ID":7,"firstName":"jay","middleName":"a","lastName":"blue","ContactType":{"__KEY":"2","__STAMP":4,"ID":2,"name":"Home","contactCollection":{"__deferred":{"uri":"/rest/ContactType(2)/contactCollection?$expand=contactCollection"}}}}]}
这是调用读取并绑定到网格的代码
var ContactDS = GetContactDS();
$("#grid").kendoGrid({
selectable: "row",
filterable: true,
pageable: true,
sortable: true,
change: function(){
datamodel = this.dataItem(this.select());
ID = datamodel.ID
},
dataSource: ContactDS,
columns: [
{ field: "ID" },
{ field: "firstName" },
{ field: "middleName" },
{ field: "lastName" },
{field: "ContactType.name"}
]
});
哪个工作正常我在我的数据源中获得了 ContactType 的扩展信息,它可以很好地绑定到网格。现在我想在将所选数据行读入表单后更新,将数据读入表单工作正常。
问题是将更新发送回服务器,它期望相关实体 ContactType 的格式略有不同它只需要“__Key”的更改值来更新这是我的更新函数:
$("#update").click(function () {
datamodel.set("firstName", $("#firstName").val());
datamodel.set("lastName", $("#lastName").val());
datamodel.set("middleName", $("#middleName").val());
// datamodel.set("ContactType.__KEY",3);
ContactDS.sync();
这是服务器期望的数据
{ "__ENTITIES": [{"__KEY":"7","__STAMP":14,"firstName":"jay","middleName":"a","lastName":"red","ContactType":{"__KEY":"2"}}]}
这是 kendo.datasource 发送的内容
[{"__KEY":"7","__STAMP":12,"ID":7,"firstName":"jay","middleName":"a","lastName":"blue","ContactType":{"__KEY":"3","__STAMP":2,"ID":3,"name":"Work","contactCollection":{"__deferred":{"uri":"/rest/ContactType(3)/contactCollection?$expand=contactCollection"}}}}]
那么我该如何重新格式化数据或定义我的模型或数据源选项,以确保删除额外的 ContactType 字段,只留下更新的“_ KEY:”以及将整个请求包装在 {“ _ENTITIES”:}
谢谢你的帮助!
担