Skip to content

Instantly share code, notes, and snippets.

@martonsagi
Last active December 29, 2016 10:09
Show Gist options
  • Save martonsagi/1c8f78d8a774cc859c9ee2b1ee2c97f3 to your computer and use it in GitHub Desktop.
Save martonsagi/1c8f78d8a774cc859c9ee2b1ee2c97f3 to your computer and use it in GitHub Desktop.
Change list order in Aurelia
<template>
<require from="./list"></require>
<list items.bind="items"></list>
</template>
export class App {
items = ["Item 1", "Item 2", "Item 3"];
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
</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>
<template>
<div class="input-group" repeat.for="item of items">
${$index}
<input value.bind="item" class="input" type="text" placeholder="Item" autofocus> |
<a click.delegate="deleteItem($index)"><i class="fa fa-close"></i></a> |
<a click.delegate="moveItemUp($index, item)"><i class="fa fa-arrow-up"></i></a> |
<a click.delegate="moveItemDown($index, item)"><i class="fa fa-arrow-down"></i></a>
</div>
<a click.delegate="addItem()">Add Item</a>
import { bindable, bindingMode, BindingEngine, inject } from 'aurelia-framework';
@inject(BindingEngine)
export class List {
@bindable({defaultBindingMode: bindingMode.twoWay}) items = [];
constructor(bindingEngine) {
this.bindingEngine = bindingEngine;
}
attached() {
this.subscription = this.bindingEngine
.collectionObserver(this.items)
.subscribe(this.listChanged);
}
detached() {
this.subscription.dispose();
}
addItem() {
this.items.push(`New Item ${this.items.length + 1}`);
}
deleteItem(i) {
this.items.splice(i, 1);
}
moveItemUp(i, item) {
if (i === 0)
return;
this.moveItem(i, i - 1, item);
}
moveItemDown(i, item) {
if (i === this.items.length - 1)
return;
this.moveItem(i, i + 1, item);
}
moveItem(oldIndex, newIndex, item) {
this.items.splice(oldIndex, 1);
this.items.splice(newIndex, 0, item);
}
listChanged(slice) {
console.log("list order changed", slice);
}
}
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment