Question

I want to use secure binding with knockout. to do so I use knockout-secure-binding.js.

Who could explain why the following code does not work? it throws an error `

Uncaught #< Object > knockout-secure-binding.js:74`

after the line ko.applyBindings(new viewModel());

<html>
<head>
    <title></title>
    <script src="scripts/knockout-3.0.0-min.js"></script>
    <script src="knockout-secure-binding-master/dist/knockout-secure-binding.js"></script>

</head>
<body>
    <button type="button" data-sbind="sbtnClick">button</button>

    <script>
        var bindings = {
            sbtnClick: function () {
                return {click: this.btnClick};
            }
        };

        var viewModel = function () {
            this.btnClick = function () {
                alert('clicked');
            };
        };

        ko.bindingProvider.instance = new ko.secureBindingsProvider(bindings);
        ko.applyBindings(new viewModel());
    </script>

</body>
</html>
Was it helpful?

Solution

You still need to write out the name of the binding handler click:

<button type="button" data-sbind="click: btnClick">button</button>

And you don't need this whole bindings object with the sbtnClick, just write:

var viewModel = function () {
    this.btnClick = function () {
        alert('clicked');
    };
};

ko.bindingProvider.instance = new ko.secureBindingsProvider();
ko.applyBindings(new viewModel());

Demo JSFiddle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top