Skip to content

Instantly share code, notes, and snippets.

@swalters
Last active February 9, 2018 15:04
Show Gist options
  • Save swalters/c453ac40bf0b06d7ec5bc2e33cd885ec to your computer and use it in GitHub Desktop.
Save swalters/c453ac40bf0b06d7ec5bc2e33cd885ec to your computer and use it in GitHub Desktop.
Aurelia Dependency Injection Inheritance
<template>
<require from="./elem1"></require>
<require from="./elem2"></require>
<elem1 view-model.ref="elem1">
<h3>inside Elem1</h3>
<!-- don't process elem2 until Elem1 attached -->
<div if.bind="elem1.ready">
<elem2>
</elem2>
</div>
</elem1>
</template>
export class App {
}
import {
customElement,
inlineView,
bindable,
inject,
Container
} from 'aurelia-framework';
import {Service} from './service';
@inject(Container)
@customElement("elem1")
@inlineView("<template>Elem1 Service Name:${service.name}<slot></slot></template>")
export class Elem1 {
@bindable ready;
constructor(container) {
this.container = container;
this.ready = false;
}
bind(bindingContext) {
//I want all instances of child elements (not rendered until attached is complete)
//to return this instance of Service
//I'm trying to refactor code where I pass around instances via binding. It's becoming
//tedious as I get more and more elemenents and I have some recursion of this
//element in a component tree that should create a new Service instance.
console.log('Registering Service Instance ')
this.service = new Service('Elem1Service');
this.container.registerInstance(Service,this.service)
}
attached() {
this.ready = true;
}
}
import {
customElement,
inlineView,
bindable,
inject,
Container
} from 'aurelia-framework';
import {Service} from './service';
@customElement("elem2")
@inlineView("<template><span>Elem2 Service Name: ${service.name}</span></template>")
@inject(Service, Container)
export class Elem2 {
constructor(service, container) {
this.service = service;
//Here we see that Elem2 is injected with a new instance of service, not
//the one set in Elem1
console.log('Elem2 Service Name:' + this.service.name)
//Here we see that Elem2 container has the proper instance of Service
console.log('Elem2 get service from container. Name:' + container.get('Service').name)
}
}
<!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>
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(a => a.setRoot());
}
export class Service {
name;
constructor(name) {
this.name = name;
console.log('Created Service with Name' + name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment