syndrael
syndrael

Reputation: 158

Create custom bindings in Knockout foreach

I can't understand where i'm wrong. May you help me please ? I create a custom binding called 'bTest1', two observable data (1 array and 1 string). Applying this binding on the string works fine, but not on the array by a ko:foreach

Here's my jsfiddle link: Here

and my code

<!-- ko foreach:test -->
<label><input type="text" data-bind="bTest1:$root.test1,value:$data,valueUpdate:'afterkeydown'" />Test1</label><br/>
<!-- /ko -->
<br/>
<label><input type="text" data-bind="bTest1:test1,value:test1,valueUpdate:'afterkeydown'" />Test2</label>

and my JS code (as easy as possible):

ko.bindingHandlers.bTest1 = {
        init: function() {
            console.log('init Test1');
    },
    update: function() {
            console.log('update Test1');
    }
};


var modelView = function(){
    var self = this;
    self.test = ko.observableArray(['foreach_test','foreach_test1']);
    self.test1 = ko.observable('test_alone');
}

ko.applyBindings(new modelView());

I thank you for helping me. Best regards S.

Upvotes: 0

Views: 475

Answers (1)

Dylan Douglas
Dylan Douglas

Reputation: 128

Good morning.

An ObservableArray only tracks changes to the array itself (adding/removing items), not changes to the items it contains. So, each item in the array would need to be marked as ko.observable if you want the same results.

Hope this helps!

Upvotes: 1

Related Questions