-
-
Save riconegri/7e567ec5d7e85466186096fcf9a1c6ef to your computer and use it in GitHub Desktop.
Change list order in Aurelia
This file contains 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="./list"></require> | |
<list items.bind="items"></list> | |
</template> |
This file contains 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 { | |
items = ["Item 1", "Item 2", "Item 3"]; | |
} |
This file contains 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 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> |
This file contains 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> | |
<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> |
This file contains 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, 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); | |
} | |
} |
This file contains 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
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment