Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Last active April 20, 2018 15:05
Show Gist options
  • Save AshleyGrant/5ad8fa46f8e6e0bbf0936ce9e020a46a to your computer and use it in GitHub Desktop.
Save AshleyGrant/5ad8fa46f8e6e0bbf0936ce9e020a46a to your computer and use it in GitHub Desktop.
Working with Event Handlers
<template>
<require from="./some-element"></require>
<button click.trigger="showIt = !showIt">Toggle</button>
<div if.bind="showIt">
<some-element></some-element>
</div>
</template>
export class App {
}
<!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>
<input type="text" value.bind="myText" />
<button ref="myButton">Click Me!</button>
</template>
export class SomeElement {
myText = 'Hello';
wrappedHandler = event => this.handler(event);
attached() {
this.myButton.addEventListener('click', this.wrappedHandler);
}
detached() {
console.log('detached')
this.myButton.removeEventListener('click',this.wrappedHandler)
}
handler(event) {
alert(this.myText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment