Knockout.js安全绑定 [英] Knockout.js secure binding

查看:133
本文介绍了Knockout.js安全绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用knockout来使用安全绑定。为此,我使用 knockout-secure-binding.js

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 `


未捕获#< Object> knockout-secure-binding.js:74`

Uncaught #< Object > knockout-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>


推荐答案

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

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

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

你不需要这整个绑定对象与 sbtnClick ,只需写:

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());

演示 JSFiddle

这篇关于Knockout.js安全绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆