Skip to content

Instantly share code, notes, and snippets.

@Thanood
Last active June 1, 2016 19:15
Show Gist options
  • Select an option

  • Save Thanood/782e8648988619066b3065f6ba4655e1 to your computer and use it in GitHub Desktop.

Select an option

Save Thanood/782e8648988619066b3065f6ba4655e1 to your computer and use it in GitHub Desktop.
Aurelia - multiple select wrapper
<template>
<div>
<label><input type="checkbox" checked.bind="useDirectEvent" />use fireEvent directly on selectSome</label>
</div>
<div>
<select multiple value.bind="selectedValues" ref="sel">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
</div>
<div>
<button click.delegate="selectSome()">select 2 and 3</button>
</div>
<div>
<p repeat.for="value of selectedValues">${value}</p>
</div>
</template>
import {inject, BindingEngine} from 'aurelia-framework'
//import { BindingEngine } from 'aurelia-binding';
@inject(BindingEngine)
export class App {
selectedValues = [];
subscriptions = [];
useDirectEvent = false;
constructor(bindingEngine) {
this.bindingEngine = bindingEngine;
}
attached() {
this.subscriptions.push(this.bindingEngine.propertyObserver(this.sel, 'value').subscribe(this.valueChanged.bind(this)));
}
detached() {
this.subscriptions.forEach(sub => sub.dispose());
}
fireEvent(element: Element, name: string, data? = {}) {
let event = new CustomEvent(name, {
detail: data,
bubbles: true
});
element.dispatchEvent(event);
return event;
}
selectSome() {
[].forEach.call(this.sel.options, option => {
if (option.value === 'opt2' || option.value === 'opt3') {
option.selected = true;
}
});
if (this.useDirectEvent) {
this.fireEvent(this.sel, 'change');
}
}
valueChanged() {
console.log('valueChanged');
this.fireEvent(this.sel, 'change');
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app="main">
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/config2.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
/*******************************************************************************
* The following two lines enable async/await without using babel's
* "runtime" transformer. Uncomment the lines if you intend to use async/await.
*
* More info here: https://github.com/jdanyow/aurelia-plunker/issues/2
*/
//import regeneratorRuntime from 'babel-runtime/regenerator';
//window.regeneratorRuntime = regeneratorRuntime;
/******************************************************************************/
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
//agGrid.initialiseAgGridWithWebComponents();
aurelia.start().then(a => a.setRoot());
}
/* Styles go here */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment