Last active
August 22, 2018 13:06
-
-
Save mattduffield/4da42d83ec84537e33733bbc568fe726 to your computer and use it in GitHub Desktop.
Aurelia Gist - Custom Binding Behavior
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> | |
<require from='unique-binding-behavior'></require> | |
<h1>${title}</h1> | |
<form> | |
<label>User Name</label> | |
<input value.bind="username & unique:uniqueFunc & debounce:800"> | |
<p class="help-text">${lookupResult}</p> | |
</form> | |
</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 { | |
title = 'Custom Binding Behavior'; | |
username = ''; | |
uniqueNames = ['matt', 'kevin', 'larry']; | |
lookupResult = ''; | |
uniqueFunc; | |
constructor() { | |
this.uniqueFunc = (method, update, value) => { | |
if (this.uniqueNames.includes(value)) { | |
this.lookupResult = 'Sorry this username has already been used.'; | |
} | |
else { | |
this.lookupResult = ''; | |
} | |
update(value); | |
}; | |
} | |
} |
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> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style> | |
.panel { | |
border: 1px solid black; | |
} | |
.panel-heading { | |
background: lightblue; | |
color: white; | |
border-bottom: 1px solid black; | |
padding: 3px; | |
font-size: 20px; | |
} | |
.panel-body { | |
padding: 3px; | |
} | |
</style> | |
</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> |
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
const interceptMethods = ['updateTarget', 'updateSource', 'callSource']; export class UniqueBindingBehavior { bind(binding, scope, uniqueFunc) { let i = interceptMethods.length; while (i--) { let method = interceptMethods[i]; if (!binding[method]) { continue; } binding[`intercepted-${method}`] = binding[method]; let update = binding[method].bind(binding); binding[method] = uniqueFunc.bind(binding, method, update); } } unbind(binding, scope) { let i = interceptMethods.length; while (i--) { let method = interceptMethods[i]; if (!binding[method]) { continue; } binding[method] = binding[`intercepted-${method}`]; binding[`intercepted-${method}`] = null; } } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment