milo

Command

constructor
 Command() 

Option name Type Description
func Function

method name or function to be executed

*arguments List

parameters to be passed to method or function

Command class to implement "command pattern" - packaging ll information necessary for delayed method execution

function Command(func) { // , ... arguments
    this.init.apply(this, arguments);
}

Command instance methods

_.extendProto(Command, {
    init: Command$init,
    execute: Command$execute,
    setUndo: Command$setUndo,
    getUndo: Command$getUndo,
    undo: Command$undo,
    redo: Command$execute, // same for command, different for transaction
    setArguments: Command$setArguments,
    addArguments: Command$addArguments,
    getArguments: Command$getArguments,
    changeArguments: Command$changeArguments,
    destroy: Command$destroy,

    setComment: Command$setComment,
    getDescription: Command$getDescription
});

Command class methods

_.extend(Command, {
    create: Command$$create,
    createWithUndo: Command$$createWithUndo
});


function Command$init(func) { // , ... arguments
    check(func, Match.Optional(Function));
    this.func = func || function(){};
    this.args = _.slice(arguments, 1);    
}

Command$execute

function
 Command$execute() 

Execute command making command object available via function property.

function Command$execute(cb) {
    var result = this.func.apply(this, this.args);
    if (cb) _.defer(cb);
    return result;
}

Command$setUndo

function
 Command$setUndo() 

Option name Type Description
undoCommand Command

Set undo command for this command. This command becomes undo command for undo command (so undo command can change this command during its execution).

function Command$setUndo(undoCommand) {
    if (this[UNDO_COMMAND])
        logger.warn('Command setUndo: undo command is already set');

    this[UNDO_COMMAND] = undoCommand;
    undoCommand[UNDO_COMMAND] = this;
}

Command$getUndo

function
 Command$getUndo() 

Returns undo command of a given command

function Command$getUndo() {
    return this[UNDO_COMMAND];
}

Command$undo

function
 Command$undo() 

Executes undo command of current command

function Command$undo(cb) {
    var undoCmd = this.getUndo();
    if (! undoCmd) return logger.error('Command undo called without undo command present');
    var result = undoCmd.execute();
    if (cb) _.defer(cb);
    return result;
}

Command$setArguments

function
 Command$setArguments() 

Option name Type Description
*arguments List

Set command's arguments. If arguments were set during command's creation, this method will overwrite arguments and log warning.

function Command$setArguments() { //, ... arguments
    if (this.args && this.args.length)
        logger.warn('Command setArguments: command arguments are already set');
    this.args = _.toArray(arguments);
}


function Command$getArguments() {
    return this.args;
}


function Command$changeArguments() { //, ... arguments
    this.args = _.toArray(arguments);
}

Command$addArguments

function
 Command$addArguments() 

Option name Type Description
*arguments List

arguments list to be appended to command

Add (append) arguments to command

function Command$addArguments() { //, ... arguments
    if (! this.args) this.args = [];
    _.appendArray(this.args, arguments);
}

Command$$create

function
 Command$$create() 

Option name Type Description
func Function

method name or function to be executed

*arguments List

parameters to be passed to method or function

return Command

Commands factory. Likely ot be overridden by subclasses to implement custom logic of command construction

function Command$$create(func) { // , ... arguments
    return _.newApply(this, arguments);
}


function Command$$createWithUndo() {
    throw new Error('createWithUndo should be implemented by subsclass');
}

Command$destroy

function
 Command$destroy() 

Destroy current command (to prevent potential memory leaks when commands point to DOM elements)

function Command$destroy() {
    delete this.func;
    delete this.args;
    var undoCmd = this[UNDO_COMMAND];
    if (undoCmd) {
        delete this[UNDO_COMMAND][UNDO_COMMAND];
        delete this[UNDO_COMMAND];
        undoCmd.destroy();
    }
}


function Command$setComment(comment) {
    this.comment = comment;
}


function Command$getDescription() {
    return {
        func: this.func.name,
        comment: this.comment
    };
}