[英]How can I avoid code duplication when configuring widgets I build using JavaScript object literals?
我知道可能有一些非常小的和简单的继承或扩展解决方案,但是我希望这个问题不仅对我有好处。 而且更快。
我有例如以下代码来设置Kendo UI网格 。 我需要针对同一视图上的两个网格完全重复此代码,除了transport.read.data
对象中的一个参数不同。 我意识到我可以将model
和columns
定义分解为共享对象,但最终还是希望共享整个网格配置。 也许是一个名为myUserKendoGrid
的jQuery扩展?
$("#availableUsersGrid").kendoGrid({
dataSource: {
transport: {
read: {
url: "/Role/AvailableUsersJson",
data: { roleId: $("#Id").val() },
type: "GET"
}
},
schema: {
model: {
id: "Id",
fields: {
Id: { editable: false },
UserName: { editable: false },
EmployeeRefNum: { editable: false },
EmployeeSurname: { editable: false },
EmployeeFullNames: { editable: false }
}
}
}
},
columns: [
{ field: "UserName", title: "User Name" },
{ field: "EmployeeRefNum", title: "Emp. No." },
{ field: "EmployeeSurname", title: "Surname" },
{ field: "EmployeeFullNames", title: "Name" }
],
selectable: "multiple, row",
editable: false,
sortable: true,
pageable: true
});
您可以简单地通过jQuery的.extend()
方法克隆您的配置。 例如:
var superDuperAwesomeConfiguration = {
// moar configs go here, yo!
};
var copyCatConfigs = jQuery.extend(true, {}, superDuperAwesomeConfiguration);
// modify copyCatConfigs properties here
$("#gridA").kendoGrid(superDuperAwesomeConfiguration);
$("#gridB").kendoGrid(copyCatConfigs);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.