Skip to main content

command

CommandModel : d2/sdk/commands/command

Interface for classes that implement a business action over one or more NodeModel.

Extends: Backbone.Model
See: ProgressiveNodesComamnd

An implementation should be registered with the CommandCollection by an extension configuration.
Example (module mybundle/commands/my.command.js can be defined as)


define([
'nuc/lib/jquery',
'd2/sdk/commands/command'
], function($, CommandModel){
var MyCommandModel = CommandModel.extend({
defaults: {
signature: 'MyCommand',
name: 'Example name',
scope: 'single' // Indicates that this action could be executed on a selection of atmost one node, other possible value is 'multiple'
},

enabled: function(status, options) {
return true; // enabled always
},

execute: function(status, options) {
var firstNode = status.nodes.get(0);
console.log("The first node object is: " + firstNode.get('name');
return $.Deferred().resolve().promise(); // Here this command execution will be considered complete as soon as execute() returns
}
});

return MyCommandModel;
});

Example ( to register the command with command collection, in extension.json )

{
"d2/sdk/utils/commands": {
"extensions": {
"mybundle": {
"mybundle/commands/my.command"
}
}
}
}

commandModel.attributes : AttributeType

Required attributes. Can be defined using defaults hash, checkout the example.

Kind: instance property of CommandModel
See: Backbone.Model#attributes for more details.

commandModel.getCommandKey() ⇒ string

Get key associated wtih this command instance.

Kind: instance method of CommandModel
Returns: string - The key/signature of this command.

commandModel.isNonPromoted() ⇒ boolean

Whether this command is non-promoted.

Kind: instance method of CommandModel
Returns: boolean - true if non-promoted, false otherwise.

commandModel.enabled(status, [options]) ⇒ boolean

Whether this command should be enabled for a particular toolbar.

Kind: instance method of CommandModel
Returns: boolean - true if the command should be considered as enabled, false otherwise

ParamTypeDescription
statusCommandStatus

Object hash containing the contextual data

[options]object

Additional options that are not part of contextual data like settings

commandModel.execute(status, [options]) ⇒ jQuery.Promise

Business logic that executes as part of user interacting with this command (usually through a toolbar action).

A toast message is usually (unless suppressed) shown in the UI when execution completes and based on the state of the returned promise it can either be a success or an error taost.

Kind: instance abstract method of CommandModel
Returns: jQuery.Promise - The return promise object should be resolved to mark completion of the command execution successfully, whereas a rejection would indicate otherwise

ParamTypeDescription
statusCommandStatus

Object hash containing the contextual data

[options]object

Additional options that are not part of contextual data like settings

Example (Sample implementation of `execute` could look like)

var MyCommand = CommandModel.extend({
...

execute: function(status, options) {
var promise = $.Deferred().promise();
if(status.say === 'hello') {
console.log('I completed successfully');
promise.resolve();
} else {
console.log('I completed with failure');
promise.reject();
}

return promise;
}
});

CommandModel.extend(protoProperties, staticProperties) ⇒ function

To define a derived type from CommandModel.

Kind: static method of CommandModel
Returns: function - The derived type.

ParamTypeDescription
protoPropertiesobject

Properties attached to the prototype of derived type.

protoProperties.constructorfunction

The function to be used to construct instance of the derived type.

staticPropertiesobject

Properties statically attached to the derived type.

CommandModel~CommandStatus : Object

Type of data passed as first argument for any command execution.

Kind: inner typedef of CommandModel
Properties

NameTypeDefaultDescription
[suppressSuccessMessage]booleanfalse

Setting it to true suppresses display of a success toast when this command execution completes successfully.

[suppressFailMessage]booleanfalse

Setting it to true suppresses display of any error toast even if the command completes with error.

nodesNodeCollection

instance of NodeCollection, on which node object(s) this command is being executed.

[context]Context

Active application context

[toolitem]ToolitemModel

The menu behind this action, if any.

[originatingView]Marionette.View

The associated UI component where this command is being executed.

CommandModel~AttributeType : Object

Attribute types.

Kind: inner typedef of CommandModel
Properties

NameTypeDefaultDescription
signaturestring

A unique identifier.

namestring

Display name. Usually gets overriden by menu item's label if this CommandModel is bound to a toolbar menu item.

[scope]string"single"

How many NodeModels this command can execute on simultaneously. Possible values single and multi.