2

I have looked at a few related searches on similar issues but it doesn't matter how close I try to match the working result I cannot seem to figure out what is wrong with the following code.

<div data-bind="foreach: collections" >
    <div data-bind="text:name,click: $data.AddToCollectionB">
        <div data-bind="foreach: collectionB" >
        <input type="text" data-bind="value: name">
        </div>                   
    </div>
</div>

var kt = kt || {};

kt.itemA= function(n) {
    var self = this;
    self.name = n.name;
    self.collectionB = ko.observableArray(n.colB);

    self.AddToCollectionB = function () {
    self.collectionB.push(kt.itemB({ name: 'test-nested sub item' }));
    alert(self.name);
    alert(self.collectionB().length);
    };
};

kt.itemB= function(n) {
    var self = this;
    self.name = ko.observable(n.name);
};

kt.vm= new (function() {
    var self = this;
    self.collections= ko.observableArray([new kt.itemA({name:'item 1', colB: [new kt.itemB({name:'sub-item'})]}),new kt.itemA({name:'item 2', colB: []})]);
});

ko.applyBindings(kt.vm);

Could anyone please point out what I am doing wrong?

jsFiddle link: http://jsfiddle.net/L7uxh/31/

Thanks,

1 Answer 1

2

When you have text: name on your main div, it clobbers the content inside of it.

You would need to do something like put the name in a span inside of the div like: http://jsfiddle.net/rniemeyer/L7uxh/44/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.