knockout.js - How would I get a Knockout Click binding to respect the Knockout Enable binding on non-form elements? -


i have <span> couple of bindings on it. enable, , click.

<span data-bind="enable: buttonenabled, click: takeanaction">take action</span> 

is there custom binding handler write, or simple way of getting click binding respect (and not act) when enable binding false?

i know can add if statement in click action, great if automate action without adding logic view.

looks updated question comment. if use button or input element supports being disabled, work naturally you.

if did need use span, write quick custom binding wrap in condition check like:

ko.bindinghandlers.clickif = {     init: function(element, valueaccessor) {        var options = ko.utils.unwrapobservable(valueaccessor()),            wrappedhandler;          if (options && options.condition && typeof options.action === "function") {             wrappedhandler = function(data, event) {                if (ko.utils.unwrapobservable(options.condition)) {                   options.action.call(data, data, event);                }            };             ko.applybindingstonode(element, { click: wrappedhandler });           }          } }; 

you use like:

<span data-bind="clickif: { action: test, condition: isenabled }">test</span> 

sample: http://jsfiddle.net/rniemeyer/lztte/


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -