Aurelia delegate vs trigger: how do you know when to use delegate or trigger?

JavascriptAurelia

Javascript Problem Overview


I'm trying to learn how to work with the Aurelia framework. In doing so, I was reading the documentation here regarding their method of binding events. The documentation suggests using delegate by default. I have forked the plunkr that they provided in one of their blog posts and added a little bit to it. The full plunk is here.


app.html

<template>
    <input value.bind="pageInput" blur.delegate="showAlert()" placeholder="delegate()" />
    <input value.bind="pageInput" blur.trigger="showAlert()" placeholder="trigger()" />
    
    <button type="button" click.delegate="showAlert()">delegate()</button>
    <button type="button" click.trigger="showAlert()">trigger()</button>
</template>

app.js

export class App {
  showAlert() {
    alert('showAlert()');
  }
}

As you can see in the plunkr, the blur.trigger/click.delegate/click.trigger all fire the event, but blur.delegate doesn't.

Why is this the case?

How can you determine when .delegate isn't going to work(without manually testing it of course)?

Javascript Solutions


Solution 1 - Javascript

Use delegate except when you cannot use delegate.

Event delegation is a technique used to improve application performance. It drastically reduces the number of event subscriptions by leveraging the "bubbling" characteristic of most DOM events. With event delegation, handlers are not attached to individual elements. Instead, a single event handler is attached to a top-level node such as the body element. When an event bubbles up to this shared top-level handler the event delegation logic calls the appropriate handler based on the event's target.

To find out if event delegation can be used with a particular event, google mdn [event name] event. In fact, preceding any web platform related google search with mdn often returns a high quality result from the Mozilla Developer Network. Once you're on the event's MDN page, check whether the event bubbles. Only events that bubble can be used with Aurelia's delegate binding command. The blur, focus, load and unload events do not bubble so you'll need to use the trigger binding command to subscribe to these events.

Here's the MDN page for blur. It has further info on event delegation techniques for the blur and focus events.

Exceptions to the general guidance above:

Use trigger on buttons when the following conditions are met:
  1. You need to disable the button.
  2. The button's content is made up of other elements (as opposed to just text).

This will ensure clicks on disabled button's children won't bubble up to the delegate event handler. More info here.

Use trigger for click in certain iOS use-cases:

iOS does not bubble click events on elements other than a, button, input and select. If you're subscribing to click on a non-input element like a div and are targeting iOS, use the trigger binding command. More info here and here.

Solution 2 - Javascript

Regarding to this, blur delegate would work if Aurelia listens to the event in capture phase, but this is not doable atm in Aurelia. Wold be interesting if someone could provide some hint how to capture event in Aurelia

In that case, following will work:

<template>
    <input blur.delegate-capture='showAlert()' />
</template>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeinearydevelopmentView Question on Stackoverflow
Solution 1 - JavascriptJeremy DanyowView Answer on Stackoverflow
Solution 2 - JavascriptbigoponView Answer on Stackoverflow