Szworny Dziąch Szworny Dziąch - 1 year ago 123
HTML Question

click.delegate for ul items in AureliaJS


list defined as below:

<ul click.delegate="onListItemClick()">
<li repeat.for="suggestion of suggestions">

How do I pass suggestion object to onListItemClick? I know I can put
on each individual
element and then capture current
but won't it go against event delegation idea? I mean - then I will have multiple event handlers attached, and if so I could just go with
. I don't want to do this, because there may be tons of these

Answer Source

Put the click handler on the li element. That's the whole point of event delegation with Aurelia. It allows you to do stuff just like this without the overhead of creating a bunch of event handlers. There's just one event handler that is created by the framework for you. It delegates the calls. All of the elements click event will be set to the same function and Aurelia will handle calling your VM method.

Here's a

  You clicked suggestion id "${clickedId}"
    <li repeat.for="suggestion of suggestions" click.delegate="handleClick(suggestion)">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download