Skip to content

Instantly share code, notes, and snippets.

@lstarky
Last active March 2, 2017 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstarky/0f92128c825f65d5c928bae360254b89 to your computer and use it in GitHub Desktop.
Save lstarky/0f92128c825f65d5c928bae360254b89 to your computer and use it in GitHub Desktop.
Aurelia select list binding
<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>
export class App {
num = 2;
enums = {
"0": "Zero",
"1": "One",
"2": "Two",
"3": "Three"
};
constructor() {
this.fname = "Liam";
}
setNum(num) {
this.num = num;
}
}
<!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>
export class KeysValueConverter {
toView(obj) {
if (obj !== null && typeof obj === 'object') {
return Reflect.ownKeys(obj).filter(x => x !== '__observers__');
} else {
return null;
}
}
}
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(() => aurelia.setRoot());
}
<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>
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) + ")");
}
}
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