Skip to content

Instantly share code, notes, and snippets.

@ZoolWay
Last active April 5, 2017 13:39
Show Gist options
  • Select an option

  • Save ZoolWay/98746a84c714a16eed32da9a01f9710b to your computer and use it in GitHub Desktop.

Select an option

Save ZoolWay/98746a84c714a16eed32da9a01f9710b to your computer and use it in GitHub Desktop.
Attach/Detach by array
<template>
<require from="my-comp"></require>
<h1>Attach/Detach when comp is added/removed by array</h1>
<h2>Instances:</h2>
<div>
<div repeat.for="c of comps">
<my-comp value.bind="c"></my-comp>
</div>
</div>
<div>
<button click.delegate="removeFirst()">remove first</button>
<button click.delegate="append()">append</button>
<button click.delegate="clear()">clear/splice</button>
</div>
</template>
import { bindable, observable } from 'aurelia-framework'
export class App {
comps = ['i1','beta','last'];
getNewEntry(){
let s = 'new#' + Math.floor((Math.random() * 1000) + 1);
return s;
}
append() {
this.comps.push(this.getNewEntry())
}
removeFirst() {
if (this.comps.length < 1) return;
this.comps.splice(0, 1);
}
clear() {
this.comps.splice(0);
}
}
<!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>
<template>
<div style="border:1px dashed blue; margin: 1em;">
<span>my-comp: '${value}'</span>
</div>
</template>
import { bindable } from 'aurelia-framework'
export class MyComp {
@bindable
value = null;
attached() {
console.info(`my-comp attached: ${this.value}`);
}
detached() {
console.info(`my-comp DEtached: ${this.value}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment