我已经定义了一个带有传输方法(创建、读取、更新和销毁)的 kendo 数据源,以及一个模式定义:
$(document).ready(function() {
var viewModel = kendo.observable({
dsMedication: new kendo.data.DataSource({
type: "json",
serverFiltering: true,
serverPaging: true,
pageSize: 6,
error: function(e) {
//alert(e.responseText);
//var error_obj = $.parseJSON(e.responseText);
//if (error_obj.Message != null) {
//alert(error_obj.Message);
//}
},
transport: {
read: {
contentType: "application/json; charset=utf-8",
type: "POST",
url: "../Services/svcMedication.asmx/SearchMedication",
dataType: "json",
cache: false
},
update: {
contentType: "application/json; charset=utf-8",
type: "POST",
url: "../Services/svcMedication.asmx/SaveMedication",
dataType: "json",
cache: false
},
destroy: {
url: "../Services/svcMedication.asmx/DeleteMedication",
type: "DELETE",
dataType: "json",
cache: false
},
create: {
contentType: "application/json; charset=utf-8",
type: "POST",
url: "../Services/svcMedication.asmx/SaveMedication",
cache: false
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return kendo.stringify({ models: options.models });
}
options.MedicationParam = $('#acMedications').val();
return kendo.stringify(options);
}
},
batch: true,
schema: {
data: "d",
model: {
id: "MedicationId",
fields: {
MedicationId: {
type: "number",
editable: false // this field is not editable
},
Name: {
type: "text",
validation: { // validation rules
required: true // the field is required
}
}
}
}
}
}),
SelectedMedication: null,
HasChanges: false,
save: function() {
//if (this.SelectedMedication == null) {
//this.dsMedication.add({ MedicationId: this.get("MedicationId"), Name: this.get("Name") });
//}
this.dsMedication.sync();
this.set("HasChanges", false);
},
remove: function() {
if (confirm("Are you sure you want to delete this record?")) {
this.dsMedication.remove(this.SelectedMedication);
this.set("SelectedMedication", this.dsMedication.view()[0]);
this.change();
}
},
showForm: function() {
return this.get("SelectedMedication") !== null;
},
change: function() {
this.set("HasChanges", true);
}
});
kendo.bind($("#fmMedication"), viewModel);
});
我的表单元素包含适当的数据绑定属性到表单元素,我正在调用 kendo.bind 传递表单。目标是表单用于添加和编辑记录。
如果我搜索一个项目并对其进行更改,一切都会像魅力一样!
我的问题是:
我不确定如何编写代码来初始化表单以向数据源添加新记录。
我一直没有成功地在任何地方找到关于此的示例或资源!
任何帮助,将不胜感激!谢谢!