Skip to content

Instantly share code, notes, and snippets.

@abalmus
Forked from jdanyow/app.html
Last active April 22, 2016 14:16
Show Gist options
  • Save abalmus/7daf5227ce42921825e27640bef42fe9 to your computer and use it in GitHub Desktop.
Save abalmus/7daf5227ce42921825e27640bef42fe9 to your computer and use it in GitHub Desktop.
<template>
<form>
<label>view: <textarea value.bind="viewHtml" rows="8" cols="80"></textarea></label>
<label>view-model: <textarea value.bind="viewModelJs" rows="8" cols="80"></textarea></label>
<button type="submit" click.delegate="submit()">Submit</button>
<button type="button" click.delegate="remove()">Remove</button>
</form>
<div ref="container"></div>
</template>
import {inject} from 'aurelia-framework';
import {ViewFactory} from './view-factory';
@inject(ViewFactory)
export class App {
viewHtml =
`<template>
<my-element></my-element>
<h1>\${message}</h1>
</template>`;
viewModelJs =
`{
message: 'hello world',
}`;
constructor(viewFactory) {
this.viewFactory = viewFactory
}
submit() {
let viewModel = null;
this.remove();
try {
eval('viewModel = ' + this.viewModelJs);
}
catch(e) {
this.container.innerHTML = '<em style="color:red">Error parsing view model.</em>';
return;
}
this.dispose = this.viewFactory.insert(this.container, this.viewHtml, viewModel);
}
remove() {
if (this.dispose) {
this.dispose();
this.dispose = null;
}
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app="main">
<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>
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.globalResources('./my-element');
aurelia.start().then(a => a.setRoot());
}
<template>
My Element
</template>
export class MyElement {
}
textarea {
display: block;
}
import {
inject,
ViewCompiler,
ViewResources,
Container,
ViewSlot,
createOverrideContext
} from 'aurelia-framework';
@inject(ViewCompiler, ViewResources, Container)
export class ViewFactory {
constructor(viewCompiler, resources, container) {
this.viewCompiler = viewCompiler;
this.resources = resources;
this.container = container;
}
insert(containerElement, html, viewModel) {
let viewFactory = this.viewCompiler.compile(html);
let view = viewFactory.create(this.container);
let anchorIsContainer = true;
let viewSlot = new ViewSlot(containerElement, anchorIsContainer);
viewSlot.add(view);
view.bind(viewModel, createOverrideContext(viewModel));
return () => {
viewSlot.remove(view);
view.unbind();
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment