Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/reference/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ highlight(event) {
}
```

## Missing Action Handlers

If the method specified by an action descriptor doesn't exist on the controller, Stimulus will call the special `actionHandlerMissing` method. By default, `actionHandlerMissing` throws an error for the missing method, but you can override it in your controller to dynamically handle events. The method receives the action and the event as arguments:
```javascript
actionHandlerMissing(action, event) {
if (action.methodName === "next") {
// ...
}
}
```

## Naming Conventions

Always use camelCase to specify action names, since they map directly to methods on your controller.
Expand Down
8 changes: 7 additions & 1 deletion src/core/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ export class Binding {

get method(): Function {
const method = (this.controller as any)[this.methodName]

if (typeof method == "function") {
return method
}
throw new Error(`Action "${this.action}" references undefined method "${this.methodName}"`)

const action = this.action

return function (this: Controller, event: ActionEvent) {
this.actionHandlerMissing(action, event)
}
}

private applyEventModifiers(event: Event): boolean {
Expand Down
6 changes: 6 additions & 0 deletions src/core/controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Action } from "./action"
import type { ActionEvent } from "./action_event"
import { Application } from "./application"
import { ClassPropertiesBlessing } from "./class_properties"
import { Constructor } from "./constructor"
Expand Down Expand Up @@ -100,4 +102,8 @@ export class Controller<ElementType extends Element = Element> {
target.dispatchEvent(event)
return event
}

actionHandlerMissing(action: Action, _event: ActionEvent) {
throw new Error(`Action "${action}" references undefined method "${action.methodName}"`)
}
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Action } from "./action"
export { ActionEvent } from "./action_event"
export { Application } from "./application"
export { Context } from "./context"
Expand Down
5 changes: 5 additions & 0 deletions src/tests/controllers/log_controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Action } from "../../core/action"
import { ActionEvent } from "../../core/action_event"
import { Controller } from "../../core/controller"

Expand Down Expand Up @@ -56,6 +57,10 @@ export class LogController extends Controller {
event.stopImmediatePropagation()
}

actionHandlerMissing(action: Action, event: ActionEvent) {
this.recordAction(`${action.methodName}_missing`, event)
}

get actionLog() {
return (this.constructor as typeof LogController).actionLog
}
Expand Down
7 changes: 6 additions & 1 deletion src/tests/modules/core/action_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class ActionTests extends LogControllerTestCase {
fixtureHTML = `
<div data-controller="c" data-action="keydown@window->c#log">
<button data-action="c#log"><span>Log</span></button>
<div id="outer" data-action="click->c#log">
<div id="outer" data-action="click->c#log mousedown->c#iAmMissing">
<div id="inner" data-controller="c" data-action="click->c#log keyup@window->c#log"></div>
</div>
<div id="multiple" data-action="click->c#log click->c#log2 mousedown->c#log"></div>
Expand Down Expand Up @@ -66,4 +66,9 @@ export default class ActionTests extends LogControllerTestCase {
await this.triggerEvent("#svgChild", "mousedown")
this.assertActions({ name: "log", eventType: "click" }, { name: "log", eventType: "mousedown" })
}

async "test missing actions routed through actionHandlerMissing"() {
await this.triggerEvent("#outer", "mousedown")
this.assertActions({ name: "iAmMissing_missing", eventType: "mousedown" })
}
}