Last active
March 2, 2018 08:17
-
-
Save djedi/305f3752a56d1b3440ebfcaa3933c652 to your computer and use it in GitHub Desktop.
collection observer vs observable, splice, replace combo solution
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 click.delegate="addData()">Add Data</button> | |
<button click.delegate="popData()">Pop Data</button> | |
<button click.delegate="spliceData()">Splice Data</button> | |
<button click.delegate="replaceData()">Replace Data</button> | |
<hr> | |
Data: | |
<table> | |
<tr repeat.for="val of data"> | |
<td>${val}</td> | |
</tr> | |
</table> | |
<hr> | |
Log: | |
<ul> | |
<li repeat.for="entry of log">${entry}</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 {BindingEngine, inject, observable} from 'aurelia-framework'; | |
@inject(BindingEngine) | |
export class App { | |
@observable data = []; | |
constructor(bindingEngine) { | |
this.bindingEngine = bindingEngine; | |
this.log = []; | |
} | |
attached() { | |
this.observeData(); | |
} | |
observeData() { | |
if (this.subscription) { | |
this.subscription.dispose(); | |
} | |
this.subscription = this.bindingEngine | |
.collectionObserver(this.data) | |
.subscribe(splices => { | |
this.dataModified(splices); | |
}); | |
} | |
detached() { | |
if (this.subscription) { | |
this.subscription.dispose(); | |
} | |
} | |
dataChanged(newVal) { | |
this.log.unshift('data changed ' + new Date()); | |
this.observeData(); | |
} | |
dataModified(splices) { | |
this.log.unshift('data modified ' + new Date()); | |
} | |
addData() { | |
this.data.push(new Date()); | |
} | |
popData() { | |
this.data.pop(); | |
} | |
spliceData() { | |
const rand = Math.floor(Math.random() * this.data.length); | |
this.data.splice(rand, 1, 'spliced'); | |
} | |
replaceData() { | |
this.data = ['replaced', 'data', 'completely']; | |
} | |
} |
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://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