Skip to content

Bug using "with binding", get "You cannot apply bindings multiple times to the same element" error message #2285

@epabon

Description

@epabon
<div  data-bind="with: $root.getData.bind($data)">
    <div>
        <div data-bind="foreach: $root.items.sort(function (l, r) { return l.Value > r.Value ? 1 : -1 })">
            <div data-bind="text: Name"></div>
        </div>
    </div>
    <div data-bind="style: { 'color': Levels[$data].BackgroundColor }, text: Levels[$data].Name"></div>
</div>

In the view model:

self.getData = function () {
        var value = 0;
        var count = 0;
        for (var i = 0, length = this.Children().length; i < length; i++) {
            var child = this.Children()[i];
            value += child.Level.Value();
            count++;
        }
        return count == 0 ? 0 : Math.ceil(value / count);
}

This return the error message "You cannot apply bindings multiple times to the same element" when a Level Value changes. If I change the binding to: data-bind="with: $root.getData($data)" the error disappears but when the getData return 0 the binging change it to null or undefined.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions