Skip to content

Commit

Permalink
Added the 'command' binding handler, similar to ICommand interface in…
Browse files Browse the repository at this point in the history
… xaml.

It's a combination of the 'click' and 'enable' bindings. It allows a more compact and clear syntax.
Don't know if it can go for the standard but I think it can be usefull to someone else.
  • Loading branch information
gigi81 committed Dec 21, 2011
1 parent c42966c commit 877da04
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/binding/defaultBindings.js
Expand Up @@ -465,6 +465,25 @@ ko.bindingHandlers['hasfocus'] = {
} }
}; };


// "command: functionName" is a combination of the 'click' and 'enable' bindings. this is similar to xaml ICommand interface
// functionName should support a functionName.enabled() or functionName.canExecute() to enable/disable the element it's bound to
ko.bindingHandlers['command'] = {
'init': function (element, valueAccessor, allBindingsAccessor, viewModel) {
ko.bindingHandlers['click']['init'](element, valueAccessor, allBindingsAccessor, viewModel);
},
'update': function (element, valueAccessor, allBindingAccessor, viewModel) {
ko.bindingHandlers['enable']['update'](element, function () {
var command = ko.utils.unwrapObservable(valueAccessor());
if (command.enabled)
return jQuery.isFunction(command.enabled) ? command.enabled() : command.enabled;
if(command.canExecute)
return jQuery.isFunction(command.canExecute) ? command.canExecute() : command.canExecute;

return true;
});
}
};

// "with: someExpression" is equivalent to "template: { if: someExpression, data: someExpression }" // "with: someExpression" is equivalent to "template: { if: someExpression, data: someExpression }"
ko.bindingHandlers['with'] = { ko.bindingHandlers['with'] = {
makeTemplateValueAccessor: function(valueAccessor) { makeTemplateValueAccessor: function(valueAccessor) {
Expand Down

0 comments on commit 877da04

Please sign in to comment.