Last active
August 18, 2017 07:52
-
-
Save petmat/d7c39b0f4b96733686abd4bb660a8df3 to your computer and use it in GitHub Desktop.
Aurelia two-way binding problem
This file contains hidden or 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="./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> |
This file contains hidden or 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 { 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}`); | |
} | |
} |
This file contains hidden or 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"> | |
</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> |
This file contains hidden or 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> | |
<button type="button" click.delegate="clearNodes()">Clear nodes</button> | |
<ul> | |
<li repeat.for="node of nodes">${node}</li> | |
</ul> | |
</template> |
This file contains hidden or 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, 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