-
-
Save ielcoro/1972185a29777a6094fa8f4e3c73452a to your computer and use it in GitHub Desktop.
Aurelia view engine hooks
This file contains 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> | |
<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> |
This file contains 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 { | |
constructor() { | |
setTimeout(1000); | |
this.myPromise = new Promise(resolve => setTimeout(() => resolve('hello world!'), 2000)); | |
} | |
} |
This file contains 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
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; | |
} | |
}; |
This file contains 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> | |
<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