Skip to content

Instantly share code, notes, and snippets.

@len-ro
Last active September 9, 2016 16:10
Show Gist options
  • Save len-ro/ad17c109551c580a8e8b5dc268583414 to your computer and use it in GitHub Desktop.
Save len-ro/ad17c109551c580a8e8b5dc268583414 to your computer and use it in GitHub Desktop.
Dynamic using ViewCompiler
<template>
<button type="button" click.delegate="load($event)">Load</button>
<div ref="formContainer"></div>
</template>
import {HttpClient} from 'aurelia-fetch-client';
import {inject} from 'aurelia-framework';
import {ViewFactory} from './view-factory'
@inject(HttpClient, ViewFactory)
export class App {
model = {name: 'John', age: 40 };
constructor(http:HttpClient, viewFactory:ViewFactory){
this.http = http;
this.viewFactory = viewFactory;
}
load(event){
return this.http.fetch('dynamic.html')
.then(response => response.text())
.then(htmlAsText => {
this.viewFactory.insert(this.formContainer, htmlAsText, this);
});
}
}
<template>
<input value.bind="model.name"/>
<input type="number" value.bind="model.age"/>
</template>
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2016.1.226/styles/kendo.common.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2016.1.226/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2016.1.226/styles/kendo.default.min.css">
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2016.1.226/styles/kendo.mobile.all.min.css">
<script src="https://kendo.cdn.telerik.com/2016.1.226/js/jszip.min.js"></script>
</head>
<body aurelia-app="main">
<h1>Loading...</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.6/system.js"></script>
<script src="https://rawgit.com/aurelia-ui-toolkits/aurelia-kendoui-bundles/0.3.5/config2.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-kendoui-bridge', kendo => kendo.pro());
aurelia.start().then(a => a.setRoot());
}
import {
inject,
ViewCompiler,
ViewResources,
Container,
ViewSlot,
createOverrideContext
} from 'aurelia-framework';
@inject(ViewCompiler, ViewResources, Container)
export class ViewFactory {
constructor(viewCompiler:ViewCompiler, resources:ViewResources, container:Container) {
this.viewCompiler = viewCompiler;
this.resources = resources;
this.container = container;
}
insert(containerElement, html, viewModel) {
let viewFactory = this.viewCompiler.compile(html, this.resources);
let view = viewFactory.create(this.container);
view.bind(viewModel, createOverrideContext(viewModel));
let viewSlot = new ViewSlot(containerElement, true);
viewSlot.add(view);
viewSlot.attached();
return () => {
viewSlot.remove(view);
view.unbind();
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment