2

Im using knockoutjs and try to setup a project including ko.mapping and a custom update binding for an observableArray. When setting value directly, the update binding fires once, when using ko.mapping.fromJS, the update binding fires twice. See Fiddle or code below:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="knockout-2.2.1.js"> </script>
    <script type="text/javascript" src="knockout.mapping.js"></script>
</head>
<body>
    <div data-bind="foreach: ObservableArray, updateBinding: ObservableArray">
        <span data-bind="text: Value"></span>
    </div>
    <script type="text/javascript">
        ko.bindingHandlers['updateBinding'] = {
            init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
                console.log("Binding Handler (Init)");
            },
            update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
                console.log("Binding Handler (Update)");
                var data = ko.utils.unwrapObservable(valueAccessor());
            }
        };

        function MainViewModel() {
            var self = this;

            self.ObservableArray = ko.observableArray();
        }

        var viewModel = new MainViewModel();

        // Fires Init + Update for ObservableArray
        ko.applyBindings(viewModel);

        // Setting directly fires Update for ObservableArray once
        viewModel.ObservableArray([{ "Value": "Lucky Luke" }]);

        // Setting via mapping fires Update for ObservableArray twice
        ko.mapping.fromJS({ "ObservableArray": [{ "Value": "Ludwig van Beethoven" }] }, {}, viewModel);
    </script>
</body>
</html>
Dresel
  • 2,375
  • 1
  • 28
  • 44
  • I am not sure what else you are doing, but this does not happen for me. Can you post a fiddle that reproduces the issue you are seeing? http://jsfiddle.net/tyrsius/Sgsh6/ – Kyeotic Feb 01 '13 at 19:05
  • 2
    It's impossible for us to give you a reasonable answer until you show us your markup as well. There are probably other factors in your markup that is causing this to happen. – Jeff Mercado Feb 01 '13 at 23:22
  • I modified the sample (the previous was indeed wrong) and made it as simple as possible to reproduce my problem. – Dresel Feb 02 '13 at 16:59

2 Answers2

2

This was a bug by knockout.mapping when embedding arrays in regular objects and is now fixed (see github).

Dresel
  • 2,375
  • 1
  • 28
  • 44
0

You don't need to use full function calling syntax in your binding for updateBinding as it's not an expression.

updateBinding: People().Records()

should be:

updateBinding: People().Records

Tweaked a bit: http://jsfiddle.net/twRFf/3/

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • That is the part where im uncertain - why is the update binding not firing at fromJS when leaving out the brackets - and why is it firing with brackets? Does the mapping plugin overwrites the initial view model where the dom is binded to? How could i prevent that? – Dresel Feb 04 '13 at 05:55
  • Try it without the mapping plugin. It's not free of issues. – WiredPrairie Feb 04 '13 at 13:46
  • I have to create a dependency inside the update binding or knockoutjs will not trigger it (see http://stackoverflow.com/a/14716216/1249506). However the question stays the same - I try to learn and understand the mapping plugin, doing the mapping by hand is not an option :) – Dresel Feb 06 '13 at 11:11
  • My point is, that the mapping plugin has a number of bugs and oddities, and this may be one of them. – WiredPrairie Feb 06 '13 at 11:42
  • You were right, it was a bug indeed and is now fixed. Thank you. – Dresel Feb 11 '13 at 06:36