-
Notifications
You must be signed in to change notification settings - Fork 0
Commanding
This module allow creation of better View Model by adding commanding concept.
define(["komvvm/commands"], function(commands) {
var myCommand = new commands.Command({
canExecute: function() { return myCondition; },
execute: function() { /* ... */ }
});
});define(["komvvm/commands"], function(commands) {
var myAsyncCommand = new commands.AsyncCommand({
canExecute: function(isExecuting) { return !isExecuting && myCondition; },
execute: function(complete) { $.ajax(...).always(complete); }
});
});<button data-bind="command: myCommand"></button>
<button data-bind="command: myAsyncCommand"></button>You can customize event :
<button data-bind="command: { click: myCommand, dblclick: myAsyncCommand }"></button>myCommand.execute();
myAsyncCommand.execute();you can pass argument to execute method :
myCommand.execute(myArg);
myAsyncCommand.execute(myArg);- type: function([$data])
- required
Function to execute when command execution is allowed. Binding handler pass $data to arguments.
- type: function
- return type: boolean
- optional
- default: function() { return true; }
Allow to customize when Command can be executed.
- type: any
- optional
Custom context to call callbacks with.
type: KnockoutComputed
Get if command can be executed.
$data: any | optional | argument to pass to execute callback.
returns: void
Execute command if possible.
- type: function(complete) | function($data, complete)
- required
Function to execute when command execution is allowed. Call complete argument when execution is complete.
Binding handler pass $data to arguments if callback contains two arguments.
- type: function(isExecuting)
- isExecuting: boolean | allow one time execution.
- returns type: boolean
- optional
- default: function() { return true; }
Allow to customize when AsyncCommand can be executed.
- type: any
- optional
Custom context to call callbacks with.
type: KnockoutComputed
Get if command can be executed.
type: KnockoutObservable
Get if command is executing.
$data: any | optional | argument to pass to execute callback.
returns: void
Execute command if possible.