简体   繁体   English

KnockoutJS值在数据绑定中切换

[英]KnockoutJS value toggling in data-bind

I have a bit of javascript: 我有一点javascript:

function ViewModel() {
    var self = this;
    self.highlight = ko.observable(true);   
}

ko.applyBindings(new ViewModel());

And the html that complements it: 和补充它的HTML:

<div data-bind="css: { highlighted: highlight }, click: highlight( !highlight() )">
    random string
</div>

What I'm trying to achieve: 我想要实现的目标:

  1. The css class 'highlighted' to only be activated when var highlight is true 只有当var highlight为true时,才会激活css类'highlight'
  2. Clicking on the div will toggle the bool value of var highlight 单击div将切换var highlight的bool值
  3. Wanted result: clicking on the div to activate/deactivate its css class 想要的结果:点击div来激活/停用它的css类

What I'm getting: 我得到了什么:

  1. The initial value of highlight is true , yet the css class starts deactivated (if I change the initial value to false , the css class is activated: which seems as if I've somehow triggered the click bind when I haven't yet clicked anything) highlight的初始值为true ,但css类开始停用(如果我将初始值更改为false ,则css类被激活:当我还没有点击任何东西时,好像我已经以某种方式触发了click绑定)
  2. The div's css class does not toggle on click div的css类不会在click上切换

I'd rather not make a new corresponding click function inside ViewModel. 我宁愿不在ViewModel中创建一个新的相应的点击功能。 I'm looking if possible for a bit of code I can place solely inline within the data-bind. 我正在寻找可能的一些代码,我可以在data-bind中单独内联。

Here's the code on JSFiddle: http://jsfiddle.net/4wt4x/1/ 这是JSFiddle上的代码: http//jsfiddle.net/4wt4x/1/

Can anyone explain what's happening and what I'm doing incorrectly? 谁能解释发生了什么以及我做错了什么?

I know it's an old question, but maybe could help someone. 我知道这是一个老问题,但也许可以帮助别人。 If you need to use toggle in a lot of places, maybe some custom binding sugar could help you: 如果您需要在很多地方使用切换,也许一些自定义绑定糖可以帮助您:

Binding: 捆绑:

ko.bindingHandlers.toggleClick = {
    init: function (element, valueAccessor) {
        var value = valueAccessor();

        ko.utils.registerEventHandler(element, "click", function () {
            value(!value());
        });
    }
};

Usage: 用法:

<div data-bind="css: { highlighted: highlight }, toggleClick: highlight">
    random string
</div>

Example: 例:

http://jsfiddle.net/A28UD/1/ http://jsfiddle.net/A28UD/1/

This approach keeps some of my views very cleaner. 这种方法使我的一些观点更加清晰。 Hope it helps. 希望能帮助到你。

Your click: highlight( !highlight() ) is incorrect. 您的click: highlight( !highlight() )不正确。 Click is going to try to execute a function, and when the binding was initialized, highlight would have returned whatever its value was and that is what click is going to try to execute ( true or false in your case). Click将尝试执行一个函数,并且当初始化绑定时,突出显示将返回其值的任何值,这将是单击将尝试执行的操作(在您的情况下为truefalse )。 You need to do something like this: 你需要做这样的事情:

In your javascript, place in your model: 在您的JavaScript中,放置在您的模型中:

self.toggleHighlight = function () { self.highlight(!self.highlight()) };

Then change the binding to say click: toggleHighlight 然后更改绑定说click: toggleHighlight

Like so: http://jsfiddle.net/KDypD/1/ 像这样: http//jsfiddle.net/KDypD/1/

You may need to adjust your highlight initial value as well to reflect how you want the page to show up initially. 您可能还需要调整突出显示的初始值,以反映您希望页面最初显示的方式。

Another option is to use a reusable custom function extension (custom function is used instead of extender because there are no parameters and it looks cleaner): 另一种选择是使用可重复使用的自定义函数扩展(使用自定义函数代替扩展器,因为没有参数,它看起来更干净):

ko.observable.fn.toggleable = function () {
    var self = this;
    self.toggle = function () {
        self(!self());
    };

    return self;
};

Usage 用法

self.highlight = ko.observable(true).toggleable(); 

Html HTML

<div data-bind="css: { highlighted: highlight }, click: highlight.toggle">
    random string
</div>

If you really want to do it inline: 如果你真的想内联它:

<div data-bind="click: highlight.bind($root, !highlight()), css: { highlighted: highlight } ">
    random string
</div>

where highlight is the boolean observable. 其中highlight是boolean observable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM