Knockout.js secure binding

可紊 提交于 2020-01-01 15:06:49

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/21279003/knockout-js-secure-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!