Skip to content

Instantly share code, notes, and snippets.

@hfitzwater
Last active June 25, 2018 15:39
Show Gist options
  • Save hfitzwater/6e1d6584df227657cfdc0bb0354e400e to your computer and use it in GitHub Desktop.
Save hfitzwater/6e1d6584df227657cfdc0bb0354e400e to your computer and use it in GitHub Desktop.
Aurelia Binding
<template>
<require from="comp"></require>
<p>
<comp items.two-way="items"></comp>
</p>
</template>
export class App {
items = [];
}
<template>
<p show.bind="!editableItems || editableItems.length === 0">
No items
</p>
<ul>
<li repeat.for="item of editableItems">
${ item.text } <button click.delegate="removeItem(item, $index)"> x </button>
</li>
</ul>
<button click.delegate="addItem()">
Add Item
</button>
</template>
import {bindable, inject} from "aurelia-framework";
import {BindingEngine} from 'aurelia-binding';
@inject( BindingEngine )
export class Comp {
@bindable items = [];
constructor( bindingEngine ) {
this.bindingEngine = bindingEngine;
this.subscriptions = [];
}
bind() {
this.editableItems = this.items.map( item => {
return {
text: item
}
});
}
attched() {
this.subscriptions.push(
this.bindingEngine.collectionObserver( this.editableItems ).subscribe( () => {
this.updateOriginalArray();
})
);
}
detached() {
this.subscriptions.forEach( sub => {
sub.dispose();
});
}
removeItem(item, index) {
this.editableItems.splice( index, 1 );
}
addItem() {
this.editableItems.push({
text: `Item ${this.editableItems.length}`
});
}
updateOriginalArray() {
this.items = this.editableItems.map( item => {
return item.text;
});
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
child {
padding: 0 10px ;
border: 1px solid black;
}
</style>
</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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment