私は、JavaScript アプリをよりスケーラブルにすることに取り組んできました。このアプリは、knockout.js を使用して、ユーザーが編集および更新できるように、ある種のデータベース アイテムのグリッドをバインドします。私は今、継承を扱い、それを継承する BaseModel とモデルを持つ道を進んでいます。私が抱えている問題は、継承するクラスによって BaseModel で定義されている計算されたオブザーバブルをオーバーライドしたいときに発生します。ユースケースはこちら。
ベース グリッドには、UI がバインドする計算されたオブザーバブルがあります。計算されたオブザーバブルは、ユーザーが入力して検索できるテキスト ボックス フィルターを使用して、オブザーバブル配列から機能します。かなり基本的です。
子クラスであるロールは、特定のタイプのグリッドであり、ビジネス要件の一部は部門用のフィルターを持つことでした。これは 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();
});
}
};
}
ここ数日、これについて多くの調査を行ってきましたが、まだ解決策を見つけていないことに驚いています。私は何か特別なことをしているような気がしませんが、誰かが何か提案をしてくれるかもしれません。私は誰に対してもオープンです!