Skip to content

Commit

Permalink
* core: support for setting command executers on bind.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Sep 4, 2024
1 parent e0d7f0c commit f07e267
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/core/src/helpers/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ function executeCommandLine(commandLine: string, options: CommandExecutionOption
return executeSingleCommandLine(commandLine, options);
}

export function bindCommands(element?: Selector, options?: {scope?: string, events?: string, execute?: CommandCallback} | CommandCallback | string): void {
export function bindCommands(element?: Selector, options?: {scope?: string, events?: string, execute?: CommandCallback, commands?: Record<string, CommandCallback>} | CommandCallback | string): void {
if (typeof options === 'string') {
options = {scope: options};
} else if (typeof options === 'function') {
options = {execute: options};
}
const {scope = '', events = 'click', execute: initialExecute} = options ?? {};
const {scope = '', events = 'click', execute: initialExecute, commands} = options ?? {};
const $element = $(element);
const dataAttr = `zui.commands.${scope}`;
if ($element.z(dataAttr)) {
Expand All @@ -165,6 +165,8 @@ export function bindCommands(element?: Selector, options?: {scope?: string, even
executeCommandLine(commandLine, {
execute: function (...params) {
initialExecute?.call(this, ...params);
const commandExecute = commands?.[this.name];
commandExecute?.call(this, ...params);
const {name} = this;
$target.trigger('command', [name, params, this]).trigger(`command:${scope ? `${name}.${scope}` : name}`, [params, this]);
if (scope) {
Expand Down

0 comments on commit f07e267

Please sign in to comment.