无法让 knockout.js 依赖的可观察链接工作

发布于 2024-12-21 04:57:29 字数 1702 浏览 1 评论 0原文

我无法让简单的 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" />&nbsp;No
        <input type="radio" name="didWork" value="Yes" data-bind="checked: didWork" />&nbsp;Yes
    </li>
    <li data-bind="visible: stillWorkingVisible">Are you still working?<br />
        <input type="radio" name="stillWorking" value="No" data-bind="checked: stillWorking" />&nbsp;No
        <input type="radio" name="stillWorking" value="Yes" data-bind="checked: stillWorking" />&nbsp;Yes
    </li>
    <li data-bind="visible: didYouQuitVisible">Did you quit?<br />
        <input type="radio" name="didYouQuit" value="No" data-bind="checked: didYouQuit" />&nbsp;No
        <input type="radio" name="didYouQuit" value="Yes" data-bind="checked: didYouQuit" />&nbsp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唠甜嗑 2024-12-28 04:57:29

您的代码在这个断言上似乎是错误的:

    return this.stillWorkingVisible() == 'Yes' 

stillWorkingVisible() 永远不会返回字符串“Yes”,而是返回 bool

编辑

fiddle 似乎通过修复可以按预期工作。

Your code seems to be wrong on this assertion:

    return this.stillWorkingVisible() == 'Yes' 

stillWorkingVisible() would never return a string 'Yes' but a bool.

Edit

The fiddle seems to work as expected with the fix.

情绪少女 2024-12-28 04:57:29

工作版本: http://jsfiddle.net/SvHxY/

stillWorkingVisible() 是返回一个布尔值

Working version: http://jsfiddle.net/SvHxY/

stillWorkingVisible() is returning a bool

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文