Skip to content

Instantly share code, notes, and snippets.

@sylvain-hamel
Last active September 7, 2016 08:49
Show Gist options
  • Save sylvain-hamel/9fae8e13e9d5e4526ab498504d9bcd51 to your computer and use it in GitHub Desktop.
Save sylvain-hamel/9fae8e13e9d5e4526ab498504d9bcd51 to your computer and use it in GitHub Desktop.
binding two values
<template>
<div>
foo.value <input type="text" value.bind="foo.value">
</div>
<div>
bar.value <input type="text" value.bind="bar.value">
</div>
<hr/>
<p>foo.value is the source, bar.value is the target</p>
<button click.trigger="start()">Start sync</button>
<button click.trigger="stop()">Stop sync</button>
</template>
import {BindingEngine, bindingMode} from 'aurelia-binding';
import {inject} from 'aurelia-framework';
import {Foo} from './foo'
import {Bar} from './bar'
@inject(BindingEngine)
export class App {
foo = new Foo();
bar = new Bar();
constructor(bindingEngine){
this.bindingEngine = bindingEngine;
console.log()
}
stop( ){
this.binding.unbind();
this.binding = undefined;
}
start(){
let expression = this.bindingEngine.createBindingExpression('value', 'value', bindingMode.twoWay);
this.binding = expression.createBinding(this.bar);
let scope = {foo: this.foo, bindingContext: this.foo};
this.binding.bind(scope);
}
}
export class Bar{
constructor(){
this.value = 50;
}
}
export class Foo{
constructor(){
this.value = 10;
}
}
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment