3

我想使用带有敲除的安全绑定。为此,我使用了knockout-secure-binding.js

谁能解释为什么下面的代码不起作用?它抛出一个错误`

未捕获的 #< 对象 > 淘汰赛-secure-binding.js:74`

行后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>
4

1 回答 1

3

您仍然需要写出绑定处理程序的名称click

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

而且您不需要bindings带有 的整个对象sbtnClick,只需编写:

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

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

演示JSFiddle

于 2014-01-22T09:51:19.410 回答