Skip to content

Instantly share code, notes, and snippets.

@Vheissu
Last active June 15, 2017 05:13
Show Gist options
  • Save Vheissu/d519afed4e237657b18701e1e6ed0b96 to your computer and use it in GitHub Desktop.
Save Vheissu/d519afed4e237657b18701e1e6ed0b96 to your computer and use it in GitHub Desktop.
Example of shared data
<template>
<router-view></router-view>
</template>
export class App {
configureRouter(config) {
config.map([
{route: '', name: 'component1', moduleId: './component1'},
{route: 'component2', name: 'component2', moduleId: './component2'},
]);
}
}
<template>
<h1>Component 1</h1>
<p>Component 1: ${dataStore.val}</p>
<button click.delegate="someMethod()">Set value to "b"</button>
<button click.delegate="navigateToComponentTwo()">Go to component 2</button>
</template>
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
import {DataStore} from './data-store';
@inject(Router, DataStore)
export class Comp1 {
constructor(router, dataStore) {
this.router = router;
this.dataStore = dataStore;
}
someMethod() {
this.dataStore.val = 'b';
console.log(`Component 1: ${this.dataStore.val}`);
}
navigateToComponentTwo() {
this.router.navigateToRoute('component2');
}
}
<template>
<h1>Component 2</h1>
<p>Component 2: ${dataStore.val}</p>
<button click.delegate="navigateToComponentOne()">Go to component 1</button>
</template>
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
import {DataStore} from './data-store';
@inject(Router, DataStore)
export class Comp2 {
constructor(router, dataStore) {
this.router = router;
this.dataStore = dataStore;
}
someMethod() {
console.log(`Component 2: ${this.dataStore.val}`);
}
navigateToComponentOne() {
this.router.navigateToRoute('component1');
}
}
export class DataStore {
constructor() {
this.val = 'a';
}
}
<!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://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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment