For the complete documentation index, see llms.txt. This page is also available as Markdown.

Delegate vs trigger

When binding to events in Aurelia, you have two primary commands to choose from: delegate and trigger. Understanding the differences between these two approaches is crucial for writing efficient and performant applications.

The Golden Rule

This simple rule covers most scenarios, but understanding why and when exceptions occur will help you make informed decisions.

Understanding Event Delegation

What is Event Delegation?

Event delegation is a technique where instead of attaching event listeners to individual elements, you attach a single event listener to a parent element (typically the document) and handle events as they bubble up from child elements.

How Aurelia's Delegate Works

<button click.delegate="handleClick()">Click me</button>
<button click.delegate="handleAnotherClick()">Click me too</button>

With delegate, Aurelia:

  1. Attaches one event listener to the document (or nearest shadow DOM boundary)

  2. When any click event occurs, it checks if the event target has a delegated handler

  3. Executes the appropriate handler for that specific element

This means hundreds of buttons only require one actual event listener, making it highly efficient.

Benefits of Delegation

  • Memory Efficient: One listener handles multiple elements

  • Performance: Fewer DOM event subscriptions

  • Dynamic Content: Works with elements added/removed dynamically

  • Event Bubbling: Leverages natural browser behavior

When Delegate Works

Most DOM events bubble up the DOM tree, making them perfect candidates for delegation:

Common Delegatable Events

  • click

  • dblclick

  • mousedown

  • mouseup

  • keydown

  • keyup

  • submit

  • change (on most form elements)

  • input

Example: Multiple Buttons with Delegation

All four buttons share a single event listener on the document, yet each executes its own handler correctly.

When to Use Trigger

Non-Bubbling Events

Some events don't bubble, which means they can't be delegated:

Common Non-Bubbling Events

  • focus

  • blur

  • load

  • unload

  • error

  • resize (on elements, not window)

  • scroll (on elements, not window)

Disabled Elements

Disabled form elements don't participate in event bubbling properly:

Complex Button Content

Buttons with complex nested content can have event propagation issues:

iOS Click Events

iOS Safari has specific requirements for click events on non-interactive elements:

Using Capture

The capture command is a specialized option for specific scenarios:

When Capture is Useful

Capture Behavior

  • Runs during the capturing phase (before the event reaches the target)

  • Executes even if event.stopPropagation() is called later

  • Useful for intercepting events before they reach child elements

Performance Comparison

Memory Usage Example

The difference becomes significant with large lists or frequently created/destroyed elements.

Performance Best Practices

  1. Default to delegate for most interactive elements

  2. Use trigger only when delegation doesn't work

  3. Avoid capture unless you specifically need capturing behavior

  4. Test on target devices, especially iOS for touch events

Event Object and preventDefault

Automatic preventDefault Behavior

Aurelia automatically calls preventDefault() on events handled with both delegate and trigger:

Accessing the Event Object

Real-World Examples

Data Table with Row Actions

Media Player Controls

Form with Complex Validation

Troubleshooting Common Issues

Event Not Firing with Delegate

Problem: Event handler not executing with delegate

Solution: Check if the event bubbles

Event Firing Multiple Times

Problem: Handler executing multiple times unexpectedly

Solution: Check for event propagation issues

iOS Touch Events Not Working

Problem: Click events not working on iOS

Solution: Use trigger and ensure proper CSS

Summary

Scenario
Recommended Command
Reason

Regular buttons

delegate

Efficient, leverages bubbling

Form inputs (change, input)

delegate

Events bubble naturally

Focus/Blur events

trigger

Events don't bubble

Disabled elements

trigger

Delegation issues with disabled elements

Complex button content

trigger

Prevents event target issues

iOS non-input elements

trigger

iOS Safari requirements

Event interception

capture

Need to catch events early

Large lists/tables

delegate

Memory and performance benefits

Remember: Use delegate except when you cannot use delegate. This approach will serve you well in most Aurelia applications while maintaining optimal performance and memory usage.

Last updated

Was this helpful?