Skip to content

Instantly share code, notes, and snippets.

@ielcoro
Forked from jdanyow/app.html
Last active February 15, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ielcoro/1972185a29777a6094fa8f4e3c73452a to your computer and use it in GitHub Desktop.
Save ielcoro/1972185a29777a6094fa8f4e3c73452a to your computer and use it in GitHub Desktop.
Aurelia view engine hooks
<template>
<require from="./binding-functions"></require>
<h1 if.bind="!promise(myPromise).settled">
....
</h1>
<h1 if.bind="promise(myPromise).resolved">
It resolved!
</h1>
<h1 if.bind="promise(myPromise).rejected">
It rejected!
</h1>
<h1 if.bind="promise(myPromise).settled">
value is: <code>${promise(myPromise).value}</code>
</h1>
</template>
export class App {
constructor() {
setTimeout(1000);
this.myPromise = new Promise(resolve => setTimeout(() => resolve('hello world!'), 2000));
}
}
const key = '__binding_function_state__';
class BindingFunctions {
toDispose = [];
promise = (promise) => {
if (!promise) {
return;
}
let state = promise[key];
if (state === undefined) {
state = {
value: undefined,
resolved: false,
rejected: false,
settled: false
};
Reflect.defineProperty(promise, key, { value: state, enumerable: false });
promise.then(
value => {
state.value = value;
state.resolved = true;
state.settled = true;
},
value => {
state.value = value;
state.rejected = true;
state.settled = true;
});
}
return state;
}
observable = (observable) => {
// todo...
// then push unsubscribe into this.toDispose
}
dispose() {
let i = this.toDispose.length;
while (i--) {
this.toDispose[i]();
}
}
}
export class BindingFunctionViewEngineHooks {
beforeBind(view) {
const bindingFunctions = new BindingFunctions();
view.bindingFunctions = bindingFunctions;
view.overrideContext.promise = bindingFunctions.promise;
view.overrideContext.observable = bindingFunctions.observable;
}
beforeUnbind(view) {
view.bindingFunctions.dispose();
view.bindingFunctions = null;
}
};
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment