所以我想通过使用Knockout 3.0中最新的preprocessing feature创建一些快捷方式来保存一些HTML编写复杂绑定的工作:
ko.bindingHandlers.shortcut = {
//init: function() {},
//update: function() {},
preprocess: function(value, name, addBinding) {
console.log(name + ': ' + value);
addBinding('click', 'function() { alert(' + name + '); }');
}
};
ko.applyBindings();
并像这样使用它:
<button data-bind="shortcut: 'hey!'">Press me</button>
它不起作用。我做错了什么?
答案 0 :(得分:1)
您的示例只想在name
周围加上引号,否则它会尝试警告变量shortcut
,这在绑定时不存在。
所以,像:
addBinding('click', 'function() { alert("' + name + '"); }');