Skip to content

Instantly share code, notes, and snippets.

@petmat
Last active August 18, 2017 07:52
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 petmat/d7c39b0f4b96733686abd4bb660a8df3 to your computer and use it in GitHub Desktop.
Save petmat/d7c39b0f4b96733686abd4bb660a8df3 to your computer and use it in GitHub Desktop.
Aurelia two-way binding problem
<template>
<require from="./tree"></require>
<h1>The two-way binding problem</h1>
<tree nodes.two-way="fruits"></tree>
<button type="button" click.delegate="changeFruits()">Change fruits</button>
<p repeat.for="message of messages">${message}</p>
</template>
import { observable } from 'aurelia-framework';
export class App {
@observable fruits = ['apple', 'orange', 'banana'];
messages = [];
messageCount = 0;
changeFruits() {
this.fruits = ['pear', 'apricot'];
}
fruitsChanged(newValue) {
console.log(`Tried to change fruits to ${newValue}`);
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
<template>
<button type="button" click.delegate="clearNodes()">Clear nodes</button>
<ul>
<li repeat.for="node of nodes">${node}</li>
</ul>
</template>
import { bindable, inject, TaskQueue } from 'aurelia-framework';
@inject(TaskQueue)
export class Tree {
@bindable nodes;
constructor(taskQueue) {
this.taskQueue = taskQueue;
}
nodesChanged(newValues) {
this.run(() => {
console.log(`We have new nodes: ${newValues}`);
this.selectNodes(newValues.filter(val => val !== 'pear'));
});
}
run(action) {
if (this.locked) { return; }
action();
}
clearNodes() {
this.nodes = [];
}
locked = false;
selectNodes(nodes) {
this.locked = true;
this.nodes = nodes.slice();
this.taskQueue.flushMicroTaskQueue();
this.locked = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment