Skip to content

13. Commands

ali badakhshan edited this page Apr 14, 2019 · 3 revisions

Commands

Commands is the concept of action and response to actions in Narik.
Whenever you click on a toolbar or button and... , a action raise and 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 want 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 an global processor, if direct host could not handle any command, sends it to 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
})