使用淘汰赛进行有条件的“点击"绑定 [英] Conditional 'click' binding with Knockout

查看:69
本文介绍了使用淘汰赛进行有条件的“点击"绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将click事件绑定到列表中的每个列表项:

I am binding the click event to every list item in a list:

        <ul class="modal-subject-list" data-bind="foreach: filteredSubjects">
            <li data-bind="click: $parent.pickSubject, css: {alreadyAddedBackground: hasBeenAdded}">
                <!-- Lots of code here -->
            </li>
        </ul>

如果'hasBeenAdded'解析为true,我想禁用click:绑定.我知道一些处理它的方法:

I want to disable the click: binding if 'hasBeenAdded' resolves to true. I know some messy ways to take care of it:

  1. 具有两个列表项,一个用于if: hasBeenAdded,另一个用于if: !hasBeenAdded.这远不是DRY
  2. 使用javascript处理此检查并保留视图-我不喜欢这样做,因为正在为应基本禁用的列表项生成不必要的标记.
  1. Have two list items, one for if: hasBeenAdded, and the other for if: !hasBeenAdded. This is far from DRY
  2. Handle this check with javascript and leave the view alone - I don't like this because unnecessary markup is being generated for list items that should be essentially disabled.

是否可以注册"clickIf"绑定?

Is there a way to register a "clickIf" binding?

推荐答案

执行此操作

    <ul class="modal-subject-list" data-bind="foreach: filteredSubjects">
        <li data-bind="click: hasBeenAdded ? null : $parent.pickSubject">
            <!-- Lots of code here -->
        </li>
    </ul>

http://jsfiddle.net/7hcj6/

阅读-

如果已添加filteredSubject,则什么也不做,否则将其添加到列表中.

If the filteredSubject has been added, do nothing, else add to the list or whatever.

这篇关于使用淘汰赛进行有条件的“点击"绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆