Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@glen-84
Forked from jdanyow/app.html
Last active September 9, 2016 07:53
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 glen-84/7de24dead50aa8d7fef05792fd8a0dd8 to your computer and use it in GitHub Desktop.
Save glen-84/7de24dead50aa8d7fef05792fd8a0dd8 to your computer and use it in GitHub Desktop.
<template>
<require from="./select2-custom-attribute"></require>
<require from="./debug"></require>
<h1>Standard Select</h1>
<select value.bind="selectedThings" style="width: 100%">
<option repeat.for="thing of things" model.bind="thing">${thing.name}</option>
</select>
<h1>Select2</h1>
<select select2 value.bind="selectedThings2" style="width: 100%" id="sel2">
<option repeat.for="thing of things" model.bind="thing.id">${thing.name}</option>
</select>
<debug></debug>
</template>
export class App {
things = [];
selectedThings = 2;
selectedThings2 = 2;
constructor() {
setTimeout(() => {
this.things = [
{ id: 0, name: 'foo' },
{ id: 1, name: 'bar'},
{ id: 2, name: 'baz'}
];
// Hack!
setTimeout(() => {
$("#sel2").trigger("change.select2");
}, 200);//
}, 2000)
}
}
import {inlineView} from 'aurelia-framework';
@inlineView('<template><pre><code>${json}</code></pre></template>')
export class Debug {
bindingContext = null;
updateJson() {
if (this.bindingContext === null) {
this.json = 'null';
} else if (this.bindingContext === undefined) {
this.json = 'undefined'
} else {
// todo: use a stringify function that can handle circular references.
this.json = JSON.stringify(this.bindingContext, null, 2);
}
}
bind(bindingContext) {
this.bindingContext = bindingContext;
this.updateJson();
this.interval = setInterval(::this.updateJson, 150);
}
unbind() {
this.bindingContext = null;
clearInterval(this.interval);
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
</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>
import {customAttribute, inject} from 'aurelia-framework';
//import $ from 'jquery';
//import select2 from 'select2'; // install the select2 jquery plugin
//import from 'select2/css/select2.min.css' // ensure the select2 stylesheet has been loaded
@customAttribute('select2')
@inject(Element)
export class Select2CustomAttribute {
constructor(element) {
this.element = element;
}
attached() {
$(this.element).select2(this.value)
.on('change', (evt) => {
if (evt.originalEvent) return;
this.element.dispatchEvent(new Event('change'))
});
}
detached() {
$(this.element).select2('destroy');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment