Created
October 10, 2016 18:06
-
-
Save fkleuver/dafbe21c90fb09aeb1bb95ff57b81774 to your computer and use it in GitHub Desktop.
Aurelia TypeScript
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> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.4.6/bluebird.min.js"></script> | |
| </head> | |
| <body aurelia-app="src/main"> | |
| <script src="https://cdn.rawgit.com/aurelia/aurelia/master/scripts/system.js"></script> | |
| <script src="https://cdn.rawgit.com/aurelia/aurelia/master/scripts/config-typescript.js"></script> | |
| <script src="https://cdn.rawgit.com/aurelia/aurelia/master/scripts/aurelia-core.min.js"></script> | |
| <script src="https://cdn.rawgit.com/aurelia/aurelia/master/scripts/aurelia-routing.min.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
| <template> | |
| <h1>${message}</h1> | |
| </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 App { | |
| message = 'Hello World'; | |
| } |
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.basicConfiguration(); | |
| aurelia.start().then(() => aurelia.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
| import { | |
| customElement, | |
| TaskQueue, | |
| bindable, | |
| ViewCompiler, | |
| ViewSlot, | |
| View, | |
| ViewResources, | |
| Container, | |
| ViewFactory, | |
| inlineView, | |
| inject, | |
| child | |
| } from "aurelia-framework"; | |
| @customElement("runtime-template") | |
| @inlineView("<template><div></div></template>") | |
| @inject(DOM.Element, TaskQueue, Container, ViewCompiler) | |
| export class RuntimeTemplate { | |
| @bindable() | |
| public html: string; | |
| @child("div") | |
| public runtimeViewAnchor: HTMLDivElement; | |
| public element: HTMLElement; | |
| private tq: TaskQueue; | |
| private container: Container; | |
| private viewCompiler: ViewCompile; | |
| private runtimeView: View; | |
| private runtimeViewSlot: ViewSlot; | |
| private runtimeViewFactory: ViewFactory; | |
| constructor(element, tq, container, viewCompiler) { | |
| this.element = <HTMLElement>element; | |
| this.tq = tq; | |
| this.container = container; | |
| this.viewCompiler = viewCompiler; | |
| } | |
| public bindingContext: any; | |
| public overrideContext: any; | |
| public bind(bindingContext: any, overrideContext: any): void { | |
| this.bindingContext = bindingContext; | |
| this.overrideContext = overrideContext; | |
| if (this.html) { | |
| this.htmlChanged(this.html, undefined); | |
| } | |
| } | |
| public unbind(): void { | |
| this.disposeView(); | |
| this.bindingContext = null; | |
| this.overrideContext = null; | |
| } | |
| public needsApply: boolean = false; | |
| public isAttached: boolean = false; | |
| public attached(): void { | |
| this.isAttached = true; | |
| if (this.needsApply) { | |
| this.needsApply = false; | |
| this.apply(); | |
| } | |
| } | |
| public detached(): void { | |
| this.isAttached = false; | |
| } | |
| private htmlChanged(newValue: string, oldValue: void): void { | |
| if (newValue) { | |
| if (this.isAttached) { | |
| this.tq.queueMicroTask(() => { | |
| this.apply(); | |
| }); | |
| } else { | |
| this.needsApply = true; | |
| } | |
| } else { | |
| if (this.isApplied) { | |
| this.disposeView(); | |
| } | |
| } | |
| } | |
| private isApplied: boolean = false; | |
| private apply(): void { | |
| if (this.isApplied) { | |
| this.disposeView(); | |
| } | |
| this.compileView(); | |
| } | |
| private disposeView(): void { | |
| if (this.runtimeViewSlot) { | |
| this.runtimeViewSlot.unbind(); | |
| this.runtimeViewSlot.detached(); | |
| this.runtimeViewSlot.removeAll(); | |
| this.runtimeViewSlot = null; | |
| } | |
| if (this.runtimeViewFactory) { | |
| this.runtimeViewFactory = null; | |
| } | |
| if (this.runtimeView) { | |
| this.runtimeView = null; | |
| } | |
| this.isApplied = false; | |
| } | |
| private compileView(): void { | |
| this.runtimeViewFactory = createViewFactory(this.viewCompiler, this.container, this.html); | |
| this.runtimeView = createView(this.runtimeViewFactory, this.container); | |
| this.runtimeViewSlot = createViewSlot(this.runtimeViewAnchor); | |
| this.runtimeViewSlot.add(this.runtimeView); | |
| this.runtimeViewSlot.bind(this.bindingContext, this.overrideContext); | |
| this.runtimeViewSlot.attached(); | |
| this.isApplied = true; | |
| } | |
| } | |
| function createViewFactory(viewCompiler: ViewCompiler, container: Container, html: string): ViewFactory { | |
| if (!html.startsWith("<template>")) { | |
| html = `<template>${html}</template>`; | |
| } | |
| let viewResources: ViewResources = container.get(ViewResources); | |
| let viewFactory = viewCompiler.compile(html, viewResources); | |
| return viewFactory; | |
| } | |
| function createView(viewFactory: ViewFactory, container: Container): View { | |
| let childContainer = container.createChild(); | |
| let view = viewFactory.create(childContainer); | |
| return view; | |
| } | |
| function createViewSlot(containerElement: Element): ViewSlot { | |
| let viewSlot = new ViewSlot(containerElement, true); | |
| return viewSlot; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment