Last active
March 2, 2017 19:04
-
-
Save lstarky/0f92128c825f65d5c928bae360254b89 to your computer and use it in GitHub Desktop.
Aurelia select list binding
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="./my-dropdown"></require> | |
<div class="container-fluid"> | |
<h1>Hello, ${fname}!</h1> | |
Number: ${num} | |
<my-dropdown value.bind="num" enum-list.bind="enums"></my-dropdown> | |
<br> | |
Change value: | |
<button click.delegate="setNum(1)">1</button> | |
<button click.delegate="setNum(2)">2</button> | |
<button click.delegate="setNum(3)">3</button> | |
</div> | |
</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 { | |
num = 2; | |
enums = { | |
"0": "Zero", | |
"1": "One", | |
"2": "Two", | |
"3": "Three" | |
}; | |
constructor() { | |
this.fname = "Liam"; | |
} | |
setNum(num) { | |
this.num = num; | |
} | |
} |
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"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
</head> | |
<body aurelia-app="main"> | |
<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
export class KeysValueConverter { | |
toView(obj) { | |
if (obj !== null && typeof obj === 'object') { | |
return Reflect.ownKeys(obj).filter(x => x !== '__observers__'); | |
} else { | |
return null; | |
} | |
} | |
} |
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 function configure(aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.developmentLogging(); | |
aurelia.start().then(() => aurelia.setRoot()); | |
} |
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="./keys-value-converter"></require> | |
<require from="./tostring-value-converter"></require> | |
<select class="form-control" value.bind="value | tostring & debounce:1000"> | |
<option repeat.for="key of enumList | keys" model.bind="key">${key} - ${enumList[key]}</option> | |
</select> | |
</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
import {bindable, bindingMode} from 'aurelia-framework'; | |
export class MyDropdown { | |
@bindable({ defaultBindingMode: bindingMode.twoWay }) value; | |
@bindable enumList; | |
// initialize | |
valueChanged(newValue, oldValue) { | |
console.log("Dropdown valueChanged from " + oldValue + " (" + (typeof oldValue) + ") to " + newValue + " (" + (typeof newValue) + ")"); | |
} | |
} |
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 TostringValueConverter { | |
toView(value) { | |
if (value === null) return null; | |
return value.toString(); | |
} | |
fromView(value) { | |
if (value === null) return null; | |
return value.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment