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)?
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.
trigger on buttons when the following conditions are met:This will ensure clicks on disabled button's children won't bubble up to the delegate event handler. More info here.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With