如何防止依赖可观察量的递归
我的 KnockoutJS 代码存在创建无限循环的问题。
假设在我的视图模型中,我有一个代表去度假的人数的值。该值绑定到一个选择列表,以便客户可以更改人数。
var viewModel = {
selectedTravellerCount: ko.observable(),
travellers: ko.observableArray([])
}
当选择列表的值发生变化(即旅行者的数量发生变化)时,我想显示一系列选择列表,以允许客户输入每个旅行者的年龄。
我假设我需要创建一个依赖于 UI 中呈现的 selectedTravellerCount
的可观察对象。
viewModel.travellerCount = ko.dependentObservable(function() {
var tQ = this.selectedTravellerCount();
alert("count:" + tQ);
for (var i = 0; i < tQ; i++) {
alert("adding");
var t = new traveller();
alert("a");
t.Age = 18;
alert("b");
this.travellers.push(t);
alert("added");
}
}, viewModel);
当页面首次呈现时,这可以正常工作。然而,第一次更改选择列表时,dependentObservable 函数会被重复调用并到达 alert("b")
行。
因此,在我看来,旅行者被推送到旅行者数组,然后导致函数再次运行,然后无限重复。
但是,我认为该函数应该仅依赖于 selectedTravellerCount
值的变化。
我缺少什么?
我也尝试过:
var viewModel = {
selectedTravellerCount: ko.observable(),
travellers: ko.observableArray([])
addTraveller: function (age) {
alert(age);
this.travellers.push({ age: age });
}
}
viewModel.travellerCount = ko.dependentObservable(function() {
var tQ = this.selectedTravellerCount();
for (var i = 0; i < tQ; i++) {
this.addTraveller(18);
}
}, viewModel);
I have an issue with my KnockoutJS code that is creating an infinite loop.
Say in my view model I have a value that represents the number of people going on a holiday. This value is bound to a select list, so that a customer can change the number of people.
var viewModel = {
selectedTravellerCount: ko.observable(),
travellers: ko.observableArray([])
}
When the value of the select list is changed (i.e. the quantity of travellers changes) I want to show an array of select lists to allow the customer to input the age of each traveller.
I assume that I need to create a observable dependent on the selectedTravellerCount
that renders in the UI.
viewModel.travellerCount = ko.dependentObservable(function() {
var tQ = this.selectedTravellerCount();
alert("count:" + tQ);
for (var i = 0; i < tQ; i++) {
alert("adding");
var t = new traveller();
alert("a");
t.Age = 18;
alert("b");
this.travellers.push(t);
alert("added");
}
}, viewModel);
This works OK when the page is first rendered. However, the first time the select list is changed, the dependentObservable function is repeatedly called and gets as far as the alert("b")
line.
So, it looks to me that the traveller is being pushed to the travellers array, which is then causing function to run again, which then gets repeated ad infinitum.
However, I thought the function should only depend on the selectedTravellerCount
value changing.
What am I missing?
I've also tried:
var viewModel = {
selectedTravellerCount: ko.observable(),
travellers: ko.observableArray([])
addTraveller: function (age) {
alert(age);
this.travellers.push({ age: age });
}
}
viewModel.travellerCount = ko.dependentObservable(function() {
var tQ = this.selectedTravellerCount();
for (var i = 0; i < tQ; i++) {
this.addTraveller(18);
}
}, viewModel);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用订阅而不是dependObservable()。每次 viewModel 更改时都会触发 dependentObservable,因此得名。添加到旅行者==更改viewModel
看看这里
订阅描述此处,位于底部。
You need to use a subscription instead of dependsObservable(). A dependsObservable is triggered each time the viewModel changes, hence the name. Adding to the travellers == changing the viewModel
take a look here
Subscriptions are described here, at the bottom.