Last active
June 25, 2018 15:39
-
-
Save hfitzwater/6e1d6584df227657cfdc0bb0354e400e to your computer and use it in GitHub Desktop.
Aurelia Binding
This file contains 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="comp"></require> | |
<p> | |
<comp items.two-way="items"></comp> | |
</p> | |
</template> |
This file contains 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
export class App { | |
items = []; | |
} |
This file contains 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> | |
<p show.bind="!editableItems || editableItems.length === 0"> | |
No items | |
</p> | |
<ul> | |
<li repeat.for="item of editableItems"> | |
${ item.text } <button click.delegate="removeItem(item, $index)"> x </button> | |
</li> | |
</ul> | |
<button click.delegate="addItem()"> | |
Add Item | |
</button> | |
</template> |
This file contains 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} from "aurelia-framework"; | |
import {BindingEngine} from 'aurelia-binding'; | |
@inject( BindingEngine ) | |
export class Comp { | |
@bindable items = []; | |
constructor( bindingEngine ) { | |
this.bindingEngine = bindingEngine; | |
this.subscriptions = []; | |
} | |
bind() { | |
this.editableItems = this.items.map( item => { | |
return { | |
text: item | |
} | |
}); | |
} | |
attched() { | |
this.subscriptions.push( | |
this.bindingEngine.collectionObserver( this.editableItems ).subscribe( () => { | |
this.updateOriginalArray(); | |
}) | |
); | |
} | |
detached() { | |
this.subscriptions.forEach( sub => { | |
sub.dispose(); | |
}); | |
} | |
removeItem(item, index) { | |
this.editableItems.splice( index, 1 ); | |
} | |
addItem() { | |
this.editableItems.push({ | |
text: `Item ${this.editableItems.length}` | |
}); | |
} | |
updateOriginalArray() { | |
this.items = this.editableItems.map( item => { | |
return item.text; | |
}); | |
} | |
} |
This file contains 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"> | |
<style> | |
child { | |
padding: 0 10px ; | |
border: 1px solid black; | |
} | |
</style> | |
</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