-
-
Save atsu85/8cdcddb33969c7fbbbb7e29718a22c6b to your computer and use it in GitHub Desktop.
Aurelia OnKeyBindingBehavior (`keyup.delegate="eventHandler(message) & onKey:'enter'"`)
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="./on-key"></require> | |
Press enter in textbox to trigger event: | |
<input | |
value.bind="message" | |
keyup.delegate="textEntered() & onKey:'enter'" | |
/> | |
${message} | |
</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 { | |
message = 'Hello World!'; | |
textEntered() { | |
alert(this.message); | |
} | |
} |
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> |
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 OnKeyBindingBehavior { | |
bind(binding, scope, ...keyCodesOrAliases) { | |
var keyCodeAliases = this.getKeyCodeAliases(); | |
if(keyCodesOrAliases.length === 0) { | |
throw new Error("Expected at least one key-code or alias that shold trigger the event"); | |
} | |
var keyCodes = keyCodesOrAliases.map(keyCodeOrAlias => { | |
if(typeof(keyCodeOrAlias) === "string") { | |
keyCodeOrAlias = keyCodeOrAlias.toLowerCase(); | |
} | |
var keyCodeAlias = keyCodeAliases[keyCodeOrAlias]; | |
return keyCodeAlias ? keyCodeAlias : keyCodeOrAlias | |
}); | |
binding.keyCodeFilteredCallSource = binding.callSource; | |
binding.callSource = function(event) { | |
if (keyCodes.indexOf(event.keyCode) !== -1) { | |
this.keyCodeFilteredCallSource(event); | |
} | |
} | |
} | |
unbind(binding, scope) { | |
binding.callSource = binding.keyCodeFilteredCallSource; | |
binding.keyCodeFilteredCallSource = null; | |
} | |
// could create a subclass to use custom aliases | |
getKeyCodeAliases() { | |
return { | |
"enter": 13, | |
"esc": 27, | |
// perhaps some more commonly used key bindings | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment