Skip to content

Instantly share code, notes, and snippets.

@niieani
Forked from jdanyow/app.html
Last active September 7, 2021 13:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niieani/bd1ee888a9311027156b29b7df6ac27e to your computer and use it in GitHub Desktop.
Save niieani/bd1ee888a9311027156b29b7df6ac27e to your computer and use it in GitHub Desktop.
Aurelia RequireJS Gist
<template>
<!-- Simple usage: -->
<h1>message: ${async(message).value}</h1>
<!-- With a placeholder: -->
<h1>message: ${async(message).value ? async(message).value : '...'}</h1>
</template>
import {asyncBindings} from './enhance-promise';
@asyncBindings
export class App {
message = new Promise((resolve) => setTimeout(() => resolve('Hello World!'), 2000));
}
function enhancePromiseForAureliaBinding(promise) {
if (!promise || typeof promise.then != 'function') return;
if (promise.hasOwnProperty('value')) return promise;
const observableGetter = function() {
return; // this is the default value of the unresolved promise
}
observableGetter.getObserver = function(promise) {
return {
subscribe: function(context, binding) {
promise.then(value => binding.updateTarget(value));
}
}
}
Object.defineProperty(promise, 'value', {
get: observableGetter,
enumerable: true,
configurable: true
})
return promise;
}
export function asyncBindings(definition) {
if (definition) {
definition.prototype.async = enhancePromiseForAureliaBinding;
}
}
<!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