Reputation: 19740
So I want to save some work writing complicated bindings in HTML by creating a few shortcuts like the following one using the latest preprocessing feature from Knockout 3.0:
ko.bindingHandlers.shortcut = {
//init: function() {},
//update: function() {},
preprocess: function(value, name, addBinding) {
console.log(name + ': ' + value);
addBinding('click', 'function() { alert(' + name + '); }');
}
};
ko.applyBindings();
And use it like this:
<button data-bind="shortcut: 'hey!'">Press me</button>
It doesn't work. What am I doing wrong?
Upvotes: 0
Views: 265
Reputation: 114792
Your sample would just want to put quotes around name
, otherwise it tries to alert on the variable shortcut
, which does not exist when bound.
So, like:
addBinding('click', 'function() { alert("' + name + '"); }');
Upvotes: 1