Knockout.js KnockoutJS:未通知可观察到
在我的ViewModel中,我具有以下属性:Knockout.js KnockoutJS:未通知可观察到,knockout.js,typescript,observable,Knockout.js,Typescript,Observable,在我的ViewModel中,我具有以下属性: logisticsDestinations: KnockoutObservableArray<Destination>; filteredlogisticsDestinations: KnockoutComputed<Destination[]>; logisticsDestinationsFilter: KnockoutObservable<string>; showNotAddressable: Knockou
logisticsDestinations: KnockoutObservableArray<Destination>;
filteredlogisticsDestinations: KnockoutComputed<Destination[]>;
logisticsDestinationsFilter: KnockoutObservable<string>;
showNotAddressable: KnockoutObservable<boolean>;
logisticsDestinations:knockoutobserray;
filteredlogisticsDestinations:敲除计算;
logisticsDestinationsFilter:KnockoutObservable;
showNotAddressable:可观察到的敲除;
在我的构造函数中,我实例化如下:
//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 });
//来自后端的数据,这是可行的
self.logisticsDestinations=ko.observearray(行);
self.showNotAddressable=ko.可观察(真);
self.logisticsDestinationsFilter=ko.可观察(“”);
self.filteredlogisticsDestinations=ko.computed(()=>{
//订阅物业
//self.showNotAddressable();
var result=self.logisticsDestinations();
if(self.logisticsDestinationsFilter().length>0){
return ko.utils.arrayFilter(self.logisticsDestinations(),
item=>(item.name.peek()
.Tolocalueppercase()
.indexOf(self.logisticsDestinationsFilter.peek().toLocaleUpperCase())>=
0));
}
如果(!self.showNotAddressable.peek()){
result=ko.utils.arrayFilter(result,item=>(item.isAddressable.peek());
}
返回结果;
}).extend({分页:10',速率限制:500});
现在,当我在logisticsDestinationsFilter
myfilteredlogisticsDestinations
中填充文本时,就会计算出我的filteredlogisticsDestinations
。但是,当我选中shownotaddress
时,它不会被计算出来
我可以通过调用self.showNotAddressable()来实现这一点代码>计算结果中的第一个。但这似乎不是一个好的工作方式,为什么它适用于过滤器而不是可寻址的
有什么想法和建议吗
谢谢!
托马斯你用“偷看”:
这意味着“只接受当前值,不接受更改”
这可以防止从订阅“showNotAddressable”可观察对象的更改中计算“filteredlogisticsDestinations”
更新1
要启用订阅,您只需编写:
self.showNotAddressable()
使用“偷看”:
这意味着“只接受当前值,不接受更改”
这可以防止从订阅“showNotAddressable”可观察对象的更改中计算“filteredlogisticsDestinations”
更新1
要启用订阅,您只需编写:
self.showNotAddressable()
您可以通过使用peek
?KO跟踪computed
中使用的可观测值,通过调用computed函数并检查其读取内容,尝试在第一个条件之前将self.showNotAddressalbe()
移动到变量,这样KO就可以看到,您正在从logisticDestinationFilter和showNotAddressable Observices获取值。那么,在不使用peek的情况下,如何获取实际值?如果我先把它放在一个var中,不幸的是它不能工作:var shownotaddress=self.shownotaddress;如果(!showNotAddressable.peek()){….
您使用peek
,通过调用computed函数并检查其读取内容,尝试移动self.showNotAddressalbe()
to variable before first if,以便KO可以看到您从logisticDestinationFilter和showNotAddressable Observices中获取值,那么我如何在不使用peek的情况下获取实际值?如果我首先将其放入var中,它将无法正常工作:var showNotAddressable=self.showNotAddressable;if(!showNotAddressable.peek()){….
太好了!谢谢。我一定已经读过了。但是我如何在不使用.peek()的情况下获取属性的值呢?self.showNotAddressable()
获取值并创建对此可观察的更改的订阅。简单地说self.showNotAddressable(),@Thomas-我已经更新了答案。太好了!谢谢。我一定已经读过了。但是我如何在不使用.peek()的情况下获取属性的值呢?self.showNotAddressable()
获取值并创建对此可观察的更改的订阅。简单地说,self.showNotAddressable(),@Thomas-我已经更新了答案。