[英]How can I avoid code duplication when configuring widgets I build using JavaScript object literals?
I know there is probably some very small and simple inheritance or extension solution for this, but I hope this question will prove beneficial for more than just me. 我知道可能有一些非常小的和简单的继承或扩展解决方案,但是我希望这个问题不仅对我有好处。 And quicker.
而且更快。
I have eg the following code to set up a Kendo UI grid . 我有例如以下代码来设置Kendo UI网格 。 I need to repeat this code exactly for two grids on the same view, except for one parameter difference in the
transport.read.data
object. 我需要针对同一视图上的两个网格完全重复此代码,除了
transport.read.data
对象中的一个参数不同。 I realise I can factor out the model
and columns
definitions into shared objects, but I would like to share the whole grid config eventually. 我意识到我可以将
model
和columns
定义分解为共享对象,但最终还是希望共享整个网格配置。 Maybe a jQuery extension called myUserKendoGrid
? 也许是一个名为
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
});
You could simply clone your configuration via the .extend()
method of jQuery . 您可以简单地通过jQuery的
.extend()
方法克隆您的配置。 For example: 例如:
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.