Skip to content

13. Commands

Ali Badakhshan edited this page Apr 15, 2019 · 3 revisions

Commands

Commands are the concept of action and response to actions in Narik.
Whenever you click on a toolbar or button and..., an action raise and the application should respond to it.

Narik Define an interface called CommandHost. Each class that want to respond to commands must implement it.

/**
 * Command info
 */
export interface CommandInfo {
  commandKey: string;
  commandData?: any;
}

export interface CommandHost {
  /**
   * An `Observable` that emit whenever state of host changes.
   */
  readonly change: Observable<any>;
  processCommand(cmd: CommandInfo);
}

And every component that wants to raise a command (toolbar and ...) should specify it's Host.
(if not specified, Narik can find default host automatically).

This approach, make it easy to extend your application. You can define toolbars in metadata, (Toolbar in MetaData)
and handle commands in Command Hosts.

Another feature is, you can define a global processor, if the direct host could not handle any command, sends it to the global processor.

To Define a global processor, create a class and extend CommandProcessor, then pass it in NarikCore ForRoot() parameters.

@Injectable()
export class DemoCommandProcessor implements CommandProcessor {
    processCommand(sender: CommandHost, cmd: CommandInfo)
    {
        ...
    }
}


NarikCoreModule.forRoot({
    configFilePath: "assets/app-config.json",
    defaultLang: "en",
    useDefaultLang: true,
    commandProcessor: DemoCommandProcessor
})