Commands

Commands

Commands

Constructor

new Commands(contextopt, CommandConstructoropt)

Adding the commands capability to an object allows to access naturaly to the undo, redo pattern.

Parameters:
Name Type Attributes Default Description
context Object <optional>
this

Future commands execution context

CommandConstructor Constructor <optional>
Command

Constructor of the command

Source:
Example
const a = {
  sum: 0,
  incr() {
    this.sum++;
  },
  decr() {
    this.sum--;
  }
};

const commands = new pattern.Commands(a);

commands.execute('incr', [], 'decr', []);
console.log(`a.sum = ${a.sum}`); // a.sum = 1;

commands.undo();
console.log(`a.sum = ${a.sum}`); // a.sum = 0;

commands.redo();
console.log(`a.sum = ${a.sum}`); // a.sum = 1;

Methods

execute(execute, executeArguments, undoopt, executeArgumentsopt)

Execute the given command

Parameters:
Name Type Attributes Description
execute String

Method to execute on the given context

executeArguments Array

List of arguments to apply on the method

undo String <optional>

Method to apply for undo

executeArguments Array <optional>

List of arguments to apply on the undo method

Source:
Example
// ...
commands.execute('incr', [], 'decr', []);

pushHistory(command) → {Commands}

Push the command to the commands history.

Parameters:
Name Type Description
command Command

Undoable command

Source:
Returns:

Commands with updated history

Type
Commands

undo()

Undo the last executed command if possible.

Source:
See: