-
-
Save loaded02/58e815ee2a6e97ece31b4ed6a4dbb264 to your computer and use it in GitHub Desktop.
Aurelia PropertyBinding
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="./child"></require> | |
<h1>Aurelia has been loaded</h1> | |
<p>Parent Object: ${getObjString()}</p> | |
<child obj.two-way="parentObj"></child> | |
</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 { | |
parentObj = { | |
name: "parent", | |
address: { | |
street: "ParentStreet", | |
city: "ParentCity" | |
} | |
} | |
constructor() { | |
setTimeout(() => { | |
console.log("changing from parent..."); | |
this.parentObj.name = "parent2"; | |
}, 2000) | |
} | |
getObjString() { | |
return JSON.stringify(this.parentObj); | |
} | |
} |
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> | |
Child Object: ${getObjString()} | |
</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, BindingEngine, bindingMode} from 'aurelia-framework'; | |
import {inject} from 'aurelia-dependency-injection'; | |
@inject(BindingEngine) | |
export class Child { | |
@bindable({defaultBindingMode: bindingMode.twoWay}) obj = { | |
name: "child", | |
address: { | |
street: "ChildStreet", | |
city: "ChildCity" | |
} | |
} | |
constructor(b) { | |
this.bindingEngine = b; | |
} | |
bind() { | |
this.subscription = this.bindingEngine | |
.propertyObserver(this.obj, "name") | |
.subscribe(this.nameChanged); | |
} | |
objChanged(newValue, oldValue) { | |
console.log(`obj changed from: ${JSON.stringify(oldValue)} to:${JSON.stringify(newValue)}`) | |
if (this.subscription) { | |
this.subscription.dispose(); | |
} | |
this.subscription = this.bindingEngine | |
.propertyObserver(this.obj, "name") | |
.subscribe(this.nameChanged); | |
} | |
getObjString() { | |
return JSON.stringify(this.obj); | |
} | |
nameChanged(newValue, oldValue) { | |
console.log(`name changed from: ${JSON.stringify(oldValue)} to:${JSON.stringify(newValue)}`) | |
} | |
} |
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"> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
</head> | |
<body aurelia-app="main"> | |
<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> |
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 function configure(aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.developmentLogging(); | |
aurelia.start().then(() => aurelia.setRoot()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment