Skip to content

Instantly share code, notes, and snippets.

@hfitzwater
Last active August 15, 2017 14:16
Show Gist options
  • Save hfitzwater/4e6144a5675bf8bd127d893cf440dab9 to your computer and use it in GitHub Desktop.
Save hfitzwater/4e6144a5675bf8bd127d893cf440dab9 to your computer and use it in GitHub Desktop.
Aurelia Custom Element Lifecycle
<template>
<div class="page-host">
<router-view></router-view>
</div>
<br>
<div>
<strong>Results:</strong>
</div>
<div repeat.for="msg of messages">
${ msg }
</div>
</template>
export class App {
configureRouter(config, router) {
config.title = 'Lifecycle';
config.map([
{ route: ['done'], name: 'done', moduleId: 'done', nav: true, title: 'Done' },
{ route: ['','lifecycle'], name: 'lifecycle', moduleId: 'lifecycle', nav: true, title: 'Lifecycle' },
]);
window.lifecycle = [];
this.messages = window.lifecycle;
this.router = router;
}
}
<template>
Home
</template>
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject( Router )
export class Done {
constructor( router ) {
this.router = router;
}
}
<!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>
<div>
Lifecycle
</div>
</template>
import {inject, customElement, TaskQueue} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@customElement( 'lifecycle' )
@inject( Router, TaskQueue )
export class Lifecyle {
constructor( router, taskQueue ) {
this.router = router;
this.taskQueue = taskQueue;
}
created( view ) {
this.log( 'created' );
}
bind( context ) {
this.log( 'bind' );
}
unbind() {
this.log( 'unbind' );
}
attached() {
this.log( 'attached' );
this.taskQueue.queueTask(() => {
this.router.navigateToRoute('done');
});
}
detached() {
this.log( 'detached' );
}
activate( params, routeConfig, navigationInstruction ) {
this.log( 'activate' );
}
deactivate() {
this.log( 'deactivate' );
}
log( msg ) {
console.log( msg );
window.lifecycle.push( msg );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment