我想使用敲除安全绑定。这样做我使用knockout-secure-binding.js。
谁能解释为什么以下代码不起作用? 它会抛出一个错误`
在行Uncaught#<对象> 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>
答案 0 :(得分: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。