3

iCheck誰かがプラグインを正しく統合するのを手伝ってくれますknockoutか? カスタムバインディングを使用してプラグインをラジオボタンに初期化しようとしたが、ビューモデルの値を更新していないためです。

HTML

<div data-bind="foreach: InstituteContactNumber">
   <div class="controls multiple-controllers">
       <input type="hidden" data-bind="value: CNoId" />
       <input class="tb-contact-no" type="text" data-bind="value: CNo" />
       &nbsp;
       **<input type="radio" name="radio-cno" 
                     data-bind="RadioButton: { checked: IsPrimary }" />**
         <i class="fa fa-trash-o ctr-btn" style="color: red;" 
               data-bind="click: $parent.RemoveContactNo, visible: $index() > 0"></i>           
   </div>
</div>

ノックアウトバインディング

ko.bindingHandlers.RadioButton = {
    init: function (element, valueAccessor) {
        //initialize icheck to the element
        $(element).iCheck({
            radioClass: 'iradio_square-blue'
        });

        $(element).on('ifChecked', function () {
            var observable = valueAccessor();
            // trying to change the observable value
            observable.checked = true;
        });
    },
    update: function (element, valueAccessor) {
        var observable = valueAccessor();
        //initially fires but it not fired when I tried to change the observable value
        //I hope that means the value has not been changed
        //anyway I have checked the model on submit, it also did not contain the values.
    }
};
4

3 に答える 3

1
//trying to change the observabe value
observable.checked = true;

オブザーバブルを設定するのではなく上書きしています。あなたが欲しい

observable.checked(true);
于 2014-01-23T05:21:34.277 に答える
0

コンポーネントまたはハンドラーで観察可能な値を自分で変更する場合は、双方向バインディングにすることを検討してください。

    ko.bindingHandlers.iCheck = {
        init: (el, valueAccessor) => {
            var observable = valueAccessor();
            $(el).on("ifChanged", function() {
                observable(this.checked);
            });
        },

        update: (el, valueAccessor) => {
            var val : boolean = ko.utils.unwrapObservable(valueAccessor());
            if (val) {
                $(el).iCheck('check');
            } else {
                $(el).iCheck('uncheck');
            }
        }
    };
于 2015-07-31T08:08:02.387 に答える