无法让 knockout.js 依赖的可观察链接工作
我无法让简单的 dependentObservable 链在 knockout.js 中工作。这是我的代码:
var viewModel = {
didWork: ko.observable(''),
stillWorking: ko.observable(''),
didYouQuit: ko.observable('')
};
viewModel.stillWorkingVisible = ko.dependentObservable(function () {
return this.didWork() == 'Yes';
}, viewModel);
viewModel.didYouQuitVisible = ko.dependentObservable(function () {
alert('ok');
return this.stillWorkingVisible() == 'Yes' && this.stillWorking() == 'No';
}, viewModel);
ko.applyBindings(viewModel);
<ul>
<li>Did you do any work?<br />
<input type="radio" name="didWork" value="No" data-bind="checked: didWork" /> No
<input type="radio" name="didWork" value="Yes" data-bind="checked: didWork" /> Yes
</li>
<li data-bind="visible: stillWorkingVisible">Are you still working?<br />
<input type="radio" name="stillWorking" value="No" data-bind="checked: stillWorking" /> No
<input type="radio" name="stillWorking" value="Yes" data-bind="checked: stillWorking" /> Yes
</li>
<li data-bind="visible: didYouQuitVisible">Did you quit?<br />
<input type="radio" name="didYouQuit" value="No" data-bind="checked: didYouQuit" /> No
<input type="radio" name="didYouQuit" value="Yes" data-bind="checked: didYouQuit" /> Yes
</li>
</ul>
每当检查“你还在工作吗”无线电时,didYouQuitVisible dependentObservable 似乎永远不会更新或触发。我在那里有一个警报(),每当选中/取消选中“你还在工作吗”无线电时,警报就不会弹出。但是,当选中/取消选中第一个无线电组时,确实会显示警报。第一个 dependentObservable (stillWorkingVisible) 工作得很好。有想法吗?
I'm having trouble getting a simple dependentObservable chain to work in knockout.js. Here is my code:
var viewModel = {
didWork: ko.observable(''),
stillWorking: ko.observable(''),
didYouQuit: ko.observable('')
};
viewModel.stillWorkingVisible = ko.dependentObservable(function () {
return this.didWork() == 'Yes';
}, viewModel);
viewModel.didYouQuitVisible = ko.dependentObservable(function () {
alert('ok');
return this.stillWorkingVisible() == 'Yes' && this.stillWorking() == 'No';
}, viewModel);
ko.applyBindings(viewModel);
<ul>
<li>Did you do any work?<br />
<input type="radio" name="didWork" value="No" data-bind="checked: didWork" /> No
<input type="radio" name="didWork" value="Yes" data-bind="checked: didWork" /> Yes
</li>
<li data-bind="visible: stillWorkingVisible">Are you still working?<br />
<input type="radio" name="stillWorking" value="No" data-bind="checked: stillWorking" /> No
<input type="radio" name="stillWorking" value="Yes" data-bind="checked: stillWorking" /> Yes
</li>
<li data-bind="visible: didYouQuitVisible">Did you quit?<br />
<input type="radio" name="didYouQuit" value="No" data-bind="checked: didYouQuit" /> No
<input type="radio" name="didYouQuit" value="Yes" data-bind="checked: didYouQuit" /> Yes
</li>
</ul>
Whenever the "are you still working" radios are checked, the didYouQuitVisible dependentObservable never seems to update or fire. I have an alert() in there, and an alert never pops up whenever the "are you still working" radios are checked/unchecked. However, the alert DOES show up when the first radio group is checked/unchecked. The first dependentObservable (stillWorkingVisible) works just fine. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码在这个断言上似乎是错误的:
stillWorkingVisible()
永远不会返回字符串“Yes”,而是返回bool
。编辑
fiddle 似乎通过修复可以按预期工作。
Your code seems to be wrong on this assertion:
stillWorkingVisible()
would never return a string 'Yes' but abool
.Edit
The fiddle seems to work as expected with the fix.
工作版本: http://jsfiddle.net/SvHxY/
stillWorkingVisible()
是返回一个布尔值Working version: http://jsfiddle.net/SvHxY/
stillWorkingVisible()
is returning a bool