Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout checked binding

So I'm trying to use a custom knockout checkbox binding to trigger visibility on some divs in a form. I'm having a difficult time figuring out why it won't work correctly. I've gotten it to the point where the initial value gets set, but then it won't reupdate. My problem is I don't seem to be able to bind the checkbox correctly.

I've got a fiddle to help this make more sense. When it loads it sets the correct values, but then subsequent clicking doesn't do anything.

I'm stumped I've looked at it for too long.

var data = true;

ko.bindingHandlers.aipchecked = {
    update: function(element, valueAccessor) {
        var options = valueAccessor();
        alert(options.value());
        if (options.value()) {
                    $(options.checked).slideDown('fast', function () { });
                    $(options.unchecked).slideUp('fast', function () { });
        } else {
                    $(options.checked).slideUp('fast', function () { });
                    $(options.unchecked).slideDown('fast', function () { });
        }
        options.value(options.value())
        //ko.bindingHandlers.checked.update(element, options.value);
      } 
};

var viewModel = {
    control:[
        {   
            checked: '#one',
            unchecked: '',
            value: ko.observable(true)
       }    
    ]
};
viewModel.control[0].value(data);
ko.applyBindings(viewModel);

The HTML

    <div data-bind="foreach: control">
<input type="checkbox" data-bind="aipchecked:{value: value,checked:checked,unchecked:unchecked}" />
    <label data-bind="text: value"></label>
</div>
    <div id="one">testing</div>

http://jsfiddle.net/gdefilippi/SuAYR/8/

V/R, Geoffrey

like image 627
gdefilippi Avatar asked May 31 '26 00:05

gdefilippi


1 Answers

Two things which are going in your code are below -

  1. Use 'checked' binding instead of 'value' binding in HTML, as the check event changes the state of the checkbox.

    <div data-bind="foreach: control">
        <input type="checkbox" data-bind="checked:value,aipchecked:{value: value,checked:checked,unchecked:unchecked}" />
        <label data-bind="text: value"></label>
    </div>
    <div id="one">testing</div>
    
  2. Remove the checked update binding from JS code.

    var data = true;
    
    ko.bindingHandlers.aipchecked = {
        update: function(element, valueAccessor) {
        var options = valueAccessor();
        alert(options.value());
        if (options.value()) {
                $(options.checked).slideDown('fast', function () { });
                $(options.unchecked).slideUp('fast', function () { });
        } else {
                $(options.checked).slideUp('fast', function () { });
                $(options.unchecked).slideDown('fast', function () { });
        }
        //ko.bindingHandlers.checked.update(element, options.value);
        } 
    };
    
    var viewModel = {
        control:[
            {   
                checked: '#one',
                unchecked: '',
                value: ko.observable(true)
            }    
        ]
    };
    viewModel.control[0].value(data);
    ko.applyBindings(viewModel);
    
like image 181
Vivek Srivastava Avatar answered Jun 02 '26 21:06

Vivek Srivastava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!