Knockoutjs - 计算可观察对象中的参考局部字段
Knockoutjs - Reference local fields in computed observable
我是 KO 的新手,在获取计算值时遇到问题。我有一个由许多对象组成的视图模型,因为我在数据库中有许多不同的表,我正在从中检索数据。这是我如何设置 VM 的示例。请随意批评我在这里做错的任何事情(尽管它确实与计算值不同):
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = ko.observable({
field1: ko.observable(object1.field1),
field2: ko.observable(object1.field2),
field3: ko.observable(object1.field3)
});
self.mySecondObject = ko.observable({
productCode: ko.observable(object2.productCode),
isActiveProduct: ko.computed(function() {
return self.activeProductCodes.indexOf(productCode) !== -1 ? true : false;
}, self)
});
}
isActiveProduct 不起作用,因为它不知道 productCode 是什么。我试过 this.productCode、self.productCode、self.mySecondObject().productCode,其中 none 似乎有效。
是否可以访问该值?我的整体 VM 设置是否有问题导致失败?
谢谢
史蒂夫
更新:
我现在不让对象可观察并声明我的虚拟机:
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = {
field1: ko.observable(object1.field1),
field2: ko.observable(object1.field2),
field3: ko.observable(object1.field3)
}
self.mySecondObject = {
productCode: ko.observable(object2.productCode),
isActiveProduct: ko.computed(function() {
return self.activeProductCodes.indexOf(productCode) !== -1 ? true : false;
}, self)
}
}
撇开评论中的简短讨论不谈,当我在敲除中处理 sub-viewmodels 时,我倾向于正确定义它们,而不是使用匿名对象。所以在你的情况下可能看起来像这样
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = ko.observable(new myFirstViewModel(object1)); // omitted for brevity
self.mySecondObject = ko.observable(new mySecondViewModel(object2, activeProductCodes ));
}
var mySecondViewModel = function(object2, activeProductCodes){
var self = this;
self.productCode = ko.observable(object2.productCode);
self.isActiveProduct = ko.computed(function() {
return activeProductCodes.indexOf(self.productCode()) !== -1 ? true : false;
}, self)
}
我还提到,您很有可能不需要子视图模型实际上是可观察的,所以您可能会逃避这个:
self.mySecondObject = new mySeocndViewModel(object2, activeProductCodes );
这取决于您的用例。
我是 KO 的新手,在获取计算值时遇到问题。我有一个由许多对象组成的视图模型,因为我在数据库中有许多不同的表,我正在从中检索数据。这是我如何设置 VM 的示例。请随意批评我在这里做错的任何事情(尽管它确实与计算值不同):
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = ko.observable({
field1: ko.observable(object1.field1),
field2: ko.observable(object1.field2),
field3: ko.observable(object1.field3)
});
self.mySecondObject = ko.observable({
productCode: ko.observable(object2.productCode),
isActiveProduct: ko.computed(function() {
return self.activeProductCodes.indexOf(productCode) !== -1 ? true : false;
}, self)
});
}
isActiveProduct 不起作用,因为它不知道 productCode 是什么。我试过 this.productCode、self.productCode、self.mySecondObject().productCode,其中 none 似乎有效。 是否可以访问该值?我的整体 VM 设置是否有问题导致失败?
谢谢
史蒂夫
更新: 我现在不让对象可观察并声明我的虚拟机:
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = {
field1: ko.observable(object1.field1),
field2: ko.observable(object1.field2),
field3: ko.observable(object1.field3)
}
self.mySecondObject = {
productCode: ko.observable(object2.productCode),
isActiveProduct: ko.computed(function() {
return self.activeProductCodes.indexOf(productCode) !== -1 ? true : false;
}, self)
}
}
撇开评论中的简短讨论不谈,当我在敲除中处理 sub-viewmodels 时,我倾向于正确定义它们,而不是使用匿名对象。所以在你的情况下可能看起来像这样
var viewModel = function(object1, object2...) {
var self = this;
var activeProductCodes = ['AB-1', 'AB-2', 'AB-3'];
self.myFirstObject = ko.observable(new myFirstViewModel(object1)); // omitted for brevity
self.mySecondObject = ko.observable(new mySecondViewModel(object2, activeProductCodes ));
}
var mySecondViewModel = function(object2, activeProductCodes){
var self = this;
self.productCode = ko.observable(object2.productCode);
self.isActiveProduct = ko.computed(function() {
return activeProductCodes.indexOf(self.productCode()) !== -1 ? true : false;
}, self)
}
我还提到,您很有可能不需要子视图模型实际上是可观察的,所以您可能会逃避这个:
self.mySecondObject = new mySeocndViewModel(object2, activeProductCodes );
这取决于您的用例。