Skip to content

Instantly share code, notes, and snippets.

@loaded02
Last active February 14, 2017 11:09
Show Gist options
  • Save loaded02/017d98f0eb71ef6d6d058e481d60d9c0 to your computer and use it in GitHub Desktop.
Save loaded02/017d98f0eb71ef6d6d058e481d60d9c0 to your computer and use it in GitHub Desktop.
Aurelia ArrayBinding
<template>
<require from="./child"></require>
<h1>Aurelia has been loaded</h1>
<p>Parent Array: ${getArray()}</p>
<child array.two-way="parentArray"></child>
</template>
export class App {
parentArray = [
{ id: 1, progress: 15 },
{ id: 2, progress: 0 }
]
constructor() {
setTimeout(() => {
console.log("changing from parent...");
//this.parentArray.push({ id: 3, progress: 50 });
this.parentArray = [
{ id: 4, progress: 23 },
{ id: 5, progress: 32 }
]
}, 2000)
}
getArray() {
return JSON.stringify(this.parentArray);
}
}
<template>
Child Array: ${getArray()}
</template>
import {bindable, BindingEngine, bindingMode} from 'aurelia-framework';
import {inject} from 'aurelia-dependency-injection';
@inject(BindingEngine)
export class Child {
@bindable({defaultBindingMode: bindingMode.twoWay}) array = []
constructor(b) {
this.bindingEngine = b;
}
bind() {
this.subscription = this.bindingEngine
.collectionObserver(this.array)
.subscribe(this.arrayBlaChanged.bind(this));
}
arrayChanged(newValue, oldValue) {
console.log(`array changed from: ${JSON.stringify(oldValue)} to:${JSON.stringify(newValue)}`)
if (this.subscription) {
this.subscription.dispose();
}
this.subscription = this.bindingEngine
.collectionObserver(this.array)
.subscribe(this.arrayBlaChanged.bind(this));
}
getArray() {
return JSON.stringify(this.array);
}
arrayBlaChanged(splices) {
console.log(`array Bla changed from: ${JSON.stringify(splices)}`)
}
}
<!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.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body aurelia-app="main">
<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>
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(() => aurelia.setRoot());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment