Trident D'Gao
Trident D'Gao

Reputation: 19740

Cannot make a shortcut for a binding using the new preprocessing feature of Knockout 3.0

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?

http://jsfiddle.net/cQvn9/3/

Upvotes: 0

Views: 265

Answers (1)

RP Niemeyer
RP Niemeyer

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

Related Questions