我一直在努力使我的JavaScript应用程序更具可扩展性。该应用程序使用knockout.js绑定某些类型的数据库项目的网格,以供用户编辑和更新。我现在正在继续使用继承,并拥有一个BaseModel和从中继承的模型。我遇到的问题出现在我想要覆盖由BaseModel中继承的类定义的计算观察值时。下面是用例。继承和重写敲除计算可观察
基本网格具有UI绑定的计算可观察值。计算出来的observable使用文本框过滤器处理可观察数组,并使用用户可以键入的文本框过滤器进行搜索。很基本。
子类,角色是特定类型的网格,部分业务需求是为部门设置过滤器。这是UI中的一个下拉菜单。我想用子类中的角色实现来覆盖基类中的计算可观察性。不知道如何做到这一点。
这里是一个的jsfiddle:http://jsfiddle.net/Icestorm0141/vx6843pt/6/
的BaseGridViewModel:
var BaseGridViewModel = function (serverData) {
var self = this;
//searchTerm is a text box that the user types to search
self.searchTerm = ko.observable();
//items are the data from the server
self.items = ko.mapping.fromJS(serverData, mapping);
//filteredItems are the filtered list of items that the grid is actually bound to
//thus when the user searches, the UI updates automatically
self.filteredItems = ko.computed(function() {
if (self.searchTerm() == null || self.searchTerm() == "" || self.searchTerm().length < 3) {
return self.items();
}
else {
return ko.utils.arrayFilter(self.items(), function (item) {
return item.Description().toLowerCase().indexOf(self.searchTerm().toLowerCase()) >= 0;
});
}
});
}
子类:
var RoleGridModel = function (roleData) {
var self = this;
//calling the base model, I think this is the proper way to do this?
BaseGridViewModel.call(self, roleData);
//UI has a drop down list of departments to filter by, this is the selected filter
self.departmentFilter = ko.observable();
//I WANT to set the filter function for the items to something completely different
var filterOverride = function() {
if (self.departmentFilter() == null || self.departmentFilter() == "") {
return self.items();
}
else {
return ko.utils.arrayFilter(self.items(), function (role) {
return role.DepartmentId() == self.departmentFilter();
});
}
};
}
我已经做了很多的研究,这最近几天,我很惊讶我还没有遇到一个解决方案。我不觉得我做的事情太特别,但也许有人有一些建议。我向任何人开放!
这应该很好。通过我的具体实现获得一些错误(我认为剑道网格试图在错误的时间绑定),但我认为一旦我解决了这个问题,这将工作得很好。谢谢! – Icestorm0141 2014-10-29 12:48:18