KnockoutJS:Observable 未被通知
KnockoutJS: Observable not being notified
在我的 ViewModel 中,我具有以下属性:
logisticsDestinations: KnockoutObservableArray<Destination>;
filteredlogisticsDestinations: KnockoutComputed<Destination[]>;
logisticsDestinationsFilter: KnockoutObservable<string>;
showNotAddressable: KnockoutObservable<boolean>;
在我的构造函数中,我这样实例化:
//Data from back-end, this works
self.logisticsDestinations = ko.observableArray<Destination>(lines);
self.showNotAddressable = ko.observable<boolean>(true);
self.logisticsDestinationsFilter = ko.observable<string>("");
self.filteredlogisticsDestinations = ko.computed(() => {
//subscribe to properties
//self.showNotAddressable();
var result = self.logisticsDestinations();
if (self.logisticsDestinationsFilter().length > 0) {
return ko.utils.arrayFilter(self.logisticsDestinations(),
item => (item.name.peek()
.toLocaleUpperCase()
.indexOf(self.logisticsDestinationsFilter.peek().toLocaleUpperCase()) >=
0));
}
if (!self.showNotAddressable.peek()) {
result = ko.utils.arrayFilter(result, item => (item.isAddressable.peek()));
}
return result;
}).extend({ paging: '10', rateLimit: 500 });
现在,当我在 logisticsDestinationsFilter
中填写文本时,我的 filteredlogisticsDestinations
得到了计算。但是,当我检查 showNotAddressable
时,它不会被计算出来。
我可以通过在计算中首先调用 self.showNotAddressable();
来让它工作。但这似乎不是一种好的工作方式,为什么它适用于过滤器而不适用于可寻址?
有什么想法、建议吗?
谢谢!
托马斯
您使用“peek”:
self.showNotAddressable.peek()
意思是“只取当前值,不订阅更改”。
这可以防止“filteredlogisticsDestinations”通过订阅“showNotAddressable”可观察对象的变化来计算。
更新 1
要启用订阅,您只需编写:
self.showNotAddressable()
在我的 ViewModel 中,我具有以下属性:
logisticsDestinations: KnockoutObservableArray<Destination>;
filteredlogisticsDestinations: KnockoutComputed<Destination[]>;
logisticsDestinationsFilter: KnockoutObservable<string>;
showNotAddressable: KnockoutObservable<boolean>;
在我的构造函数中,我这样实例化:
//Data from back-end, this works
self.logisticsDestinations = ko.observableArray<Destination>(lines);
self.showNotAddressable = ko.observable<boolean>(true);
self.logisticsDestinationsFilter = ko.observable<string>("");
self.filteredlogisticsDestinations = ko.computed(() => {
//subscribe to properties
//self.showNotAddressable();
var result = self.logisticsDestinations();
if (self.logisticsDestinationsFilter().length > 0) {
return ko.utils.arrayFilter(self.logisticsDestinations(),
item => (item.name.peek()
.toLocaleUpperCase()
.indexOf(self.logisticsDestinationsFilter.peek().toLocaleUpperCase()) >=
0));
}
if (!self.showNotAddressable.peek()) {
result = ko.utils.arrayFilter(result, item => (item.isAddressable.peek()));
}
return result;
}).extend({ paging: '10', rateLimit: 500 });
现在,当我在 logisticsDestinationsFilter
中填写文本时,我的 filteredlogisticsDestinations
得到了计算。但是,当我检查 showNotAddressable
时,它不会被计算出来。
我可以通过在计算中首先调用 self.showNotAddressable();
来让它工作。但这似乎不是一种好的工作方式,为什么它适用于过滤器而不适用于可寻址?
有什么想法、建议吗?
谢谢! 托马斯
您使用“peek”:
self.showNotAddressable.peek()
意思是“只取当前值,不订阅更改”。
这可以防止“filteredlogisticsDestinations”通过订阅“showNotAddressable”可观察对象的变化来计算。
更新 1
要启用订阅,您只需编写:
self.showNotAddressable()