Skip to content

Instantly share code, notes, and snippets.

@fabioluz
Forked from jdanyow/app.html
Created June 24, 2016 06:10
Show Gist options
  • Save fabioluz/9d5e7a60cd02e55618829a304df00621 to your computer and use it in GitHub Desktop.
Save fabioluz/9d5e7a60cd02e55618829a304df00621 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(Element, ViewFactory)
export class App {
viewHtml =
`<template>
<my-element></my-element>
<h1>\${message}</h1>
</template>`;
viewModelJs =
`{
message: 'hello world',
}`;
constructor(element, viewFactory) {
this.element = element;
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.element, 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