Skip to content

Instantly share code, notes, and snippets.

@davismj
Created August 4, 2017 10:34
Show Gist options
  • Select an option

  • Save davismj/5cb8ef96e136ab3c878c7e50d59d3e1e to your computer and use it in GitHub Desktop.

Select an option

Save davismj/5cb8ef96e136ab3c878c7e50d59d3e1e to your computer and use it in GitHub Desktop.
Custom Event Delegation
<template>
<require from="myCustomElement"></require>
<my myevent.delegate="myFunction($event)"></my>
</template>
export class App {
myFunction = (event) => alert(`myFunction called with data: ${event.detail.data}`)
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
<template>
<button click.delegate="dispatchMyEvent()">Dispatch Custom Event</button>
</template>
import { inject } from 'aurelia-framework';
@inject(Element)
export class MyCustomElement {
constructor(Element) {
this.element = Element;
}
dispatchMyEvent() {
this.element.dispatchEvent(
new CustomEvent('myevent', {
bubbles: true,
detail: {
data: 'my data'
}
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment