Controller and Action

Controller

A controller in commander-mvc is meant to act as a glue between the application logic and the view. Controllers have access to your application’s services (also called injectables) through constructor injection.

Controller(controller)

Creates a Controller decorator.

Arguments:
  • controller (ControllerInfo) – Controller information that specifies the command and its options.
Returns:

(Anonymous function) – The decorator used to designate a class a controller.

Important

In order to be recognized by the context, it must be included in ContextInitialization.providers.

class ControllerInfo()

interface, exported from ommander-mv

Meta information for a controller.

See commander.js for more information on the constructions of command and option.

ControllerInfo.command

type: string

A standard commander.js command that optionally specifies an argument. An argument in <> braces indicates it is required, while [] indicates it is optional. Within in action, the argument is accessed with this.arg.

Examples:
  • list <dir>
  • list [dir]
  • list
ControllerInfo.options

type: OptionDefinition[]

An array of option definitions. Each option definition is itself an array that will be used as the arguments to create a commander.js option.

Example:
[['-l, --list', 'list objects'], ['-s, --silent', 'be quiet']]
class TakeArg()

interface, exported from ommander-mv

Intended to be used on a controller. Use this interface when you need the takeArg method.

TakeArg.takeArg(arg)

Called when a command is given and it takes an argument.

Arguments:
  • arg (any) – The argument given to the command.
class HasArg()

interface, exported from ommander-mv

DEPRECATED. Use TakeArg() instead.

Intended to be used on a controller. Use this interface when you need the arg property.

HasArg.arg

type: any

The argument given to the command.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@Controller({
  command: 'account [budgetId]',
  options: [
    ['-l, --list', 'list account balances'],
    ['-m', '--email <address>', 'send account balances to the email address']
  ]
})
export class AccountController {
  constructor({ accountService, emailService, configService }) {
    this.accountService = accountService
    this.emailService = emailService
    this.configService = configService
  }

Action

An action in commander-mvc is an endpoint. When your application is invoked from the commandline, one of the actions defined throughout it is called.

Action(action)

Creates an Action decorator.

Arguments:
  • action (ActionInfo) – Action information that specifies the view and which options this Action will handle.
Returns:

(Anonymous function) – The decorator used to designate a method an action.

class ActionInfo()

interface, exported from ommander-mv

Meta information for an action

ActionInfo.forOptions

type: ForOptions

A function that specifies what options the action should handle. For example, ({ list, products }) => list && products, indicates the action should handle options in which --list and --products are both set. When determining which action commander-mvc will execute, it will visit each one in the order that they are defined. The first (and only the first) action for which forOptions(options) returns true will be executed. If omitted, it will be always executed when visited commander-mvc.

ActionInfo.view

type: Constructor

Constructor for the view that will render the result of the action. The return value of the action will be used the as the model and passed to the print method of the view.

Example

Continuing from the controller example above:

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  takeArg(budgetId) {
    this.budgetId = budgetId || this.configService.activeBudgetId
  }

  @Action({
    forOptions: options => options.list,
    view: AccountsView
  })
  list () {
    return this.getAccounts()
  }

  @Action({
    forOptions: options => options.email,
    view: ResultView
  })
  sendEmail ({ email }) {
    const accounts = this.getAccounts()
    return this.emailService.sendAccounts(accounts, email)
  }

  getAccounts () {
    return this.accountService.getAll(this.budgetId)
  }
}