Skip to content

Instantly share code, notes, and snippets.

@deap82
Last active January 15, 2018 08:04
Show Gist options
  • Save deap82/3e62bafe32d83b6e27cc31e0465519c7 to your computer and use it in GitHub Desktop.
Save deap82/3e62bafe32d83b6e27cc31e0465519c7 to your computer and use it in GitHub Desktop.
Aurelia Gist - router:navigation:complete issue
<template>
<ul id="menu">
<li><a href="#/Welcome">Welcome</a></li>
<li><a href="#/Products">Products</a></li>
<li><a href="#/Contact">Contact</a></li>
</ul>
<div id="content">
<router-view></router-view>
</div>
<div id="output-wrapper">
<div id="output"></div>
<button type="button" onclick="document.getElementById('output').innerHTML='';">Clear</button>
</div>
</template>
import { OutputService as output } from 'output-service';
import { EventAggregator } from 'aurelia-event-aggregator';
import { Router } from 'aurelia-router';
import { inject } from 'aurelia-framework';
@inject(EventAggregator, Router)
export class App {
router;
constructor(eventAggregator, router) {
this.router = router;
eventAggregator.subscribe('router:navigation:complete', this.routeNavigationCompleted);
}
configureRouter(config, router) {
config.map([
{ route: '', redirect: 'Welcome' },
{ route: 'Welcome', name: 'Welcome', moduleId: 'page-welcome' },
{ route: 'Products', name: 'Products', moduleId: 'page-products' },
{ route: 'Contact', name: 'Contact', moduleId: 'page-contact' },
{ route: 'ForwardToProducts', name: 'ForwardToProducts', moduleId: 'forward-to-products' }
]);
}
routeNavigationCompleted = (eventArgs, eventName) => {
var navInstruction = eventArgs.instruction;
output.log('route name from router.currentInstruction: ' + this.router.currentInstruction.config.name);
output.log('route name from eventArgs.instruction: ' + navInstruction.config.name);
}
}
import { inject, inlineView } from 'aurelia-framework';
import { Router } from 'aurelia-router';
@inject(Router)
@inlineView('<template></template>')
export class ForwardToProducts {
router;
constructor(router) {
this.router = router;
}
activate() {
this.router.navigate('Products');
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css" />
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
export class OutputService {
static log(message) {
var outputEl = document.getElementById('output');
var currentContent = outputEl.innerHTML;
outputEl.innerHTML = message + '<br />' + currentContent;
console.log(message);
}
}
<template>
<h1>Contact</h1>
</template>
import { OutputService as console } from 'output-service';
export class Contact {
}
<template>
<h1>Products</h1>
</template>
import { OutputService as console } from 'output-service';
export class Products {
}
<template>
<h1>router:navigation:complete not triggered on "redirects"</h1>
<ul>
<li>See logging in app.js:routeNavigationCompleted</li>
<li>When page is first loaded I'm expecting the eventargs to contain the same nav instruction as in router.currentInstruction.</li>
<li>When clicking "Forward me to products" below I'm also expecting the eventargs to contain the same nav instruction as in router.currentInstruction.</li>
<li>Possibly I could expect the "router:navigation:complete" event to trigger twice on these occasions, as there is a redirect happening.</li>
</ul>
<a route-href="route: ForwardToProducts">Forward me to Products</a>
</template>
import { OutputService as console } from 'output-service';
import { Router } from 'aurelia-router';
import { inject } from 'aurelia-framework';
@inject(Router)
export class Welcome {
router;
constructor(router) {
this.router = router;
}
goToProducts() {
this.router.navigate('Products');
}
}
html, html *, html *:before, html *:after {
box-sizing: border-box;
}
body {
padding: 0;
margin: 0;
font-family: sans-serif;
}
#content {
padding: 10px;
}
#menu {
background: #26418f;
color: white !important;
padding: 10px;
margin: 0;
}
#menu li {
display: inline-block;
}
#menu li + li {
margin-left: 10px;
border-left: 1px solid white;
padding-left: 10px;
}
#menu li a {
color: white;
text-decoration: none;
}
#output-wrapper {
padding: 0px 0 10px 10px;
border-top: 1px solid orange;
background: #FFFFA5;
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 150px;
font-family: Consolas;
}
#output-wrapper #output {
padding-top: 10px;
overflow-y: auto;
height: calc(100% - 30px);
}
#output-wrapper button {
position: absolute;
bottom: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment