Bug: ko.cleanNode() ignores foreach-Binding (2.0.0). #271

Closed
aspnetde opened this Issue Jan 11, 2012 · 2 comments

Comments

Projects
None yet
3 participants

Given the following example, everytime the button gets clicked a bunch of new items is added to the list. The expected behaviour is to replace the existing (and previously bound) items with the newly generated ones and update the date time.

That's really important for single-page applications.

    <div id="dynamicContentContainer">

        Bound: <span data-bind="text: date"></span>

        <ul data-bind="foreach: users">
            <li data-bind="text: firstName"></li>
        </ul>

    </div>

    <hr/>

    <button onclick="bind()">Bind</button>

    <script type="text/javascript">

        function bind() {

            var users = [];

            users.push({ firstName: "Hugo" });
            users.push({ firstName: "Alf" });
            users.push({ firstName: "Fred" });

            var model = {
                users: users,
                date: new Date()
            };

            ko.cleanNode(document.getElementById("dynamicContentContainer"));
            ko.applyBindings(model, document.getElementById("dynamicContentContainer"));

        }    

    </script>
Owner

mbest commented Feb 7, 2012

Instead of calling applyBindings multiple times, you can do this by wrapping your view model in an observable and then updating the object in the observable (see http://groups.google.com/group/knockoutjs/browse_thread/thread/1cf396bb83c0d367)

Contributor

SteveSanderson commented Feb 7, 2012

Indeed, mbest is correct - the intended usage is that ko.applyBindings should not be called multiple times on the same DOM elements. The idea is that your viewmodel updates to represent changes to state, so you won't need to be binding multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment