Last active
December 12, 2018 07:50
-
-
Save jdanyow/762c00133d5d5be624f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function configure(aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.developmentLogging() | |
.globalResources('./my-element'); | |
aurelia.start().then(a => a.setRoot()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
My Element | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MyElement { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
textarea { | |
display: block; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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