Skip to content

Instantly share code, notes, and snippets.

@Seebiscuit
Last active June 30, 2021 19:41
Show Gist options
  • Save Seebiscuit/7d6ba7fdb9457badebad6a83ea70be83 to your computer and use it in GitHub Desktop.
Save Seebiscuit/7d6ba7fdb9457badebad6a83ea70be83 to your computer and use it in GitHub Desktop.
Child takes over props
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to aurelia-bootstrapper
(data-main attribute on script) for Aurelia,
The aurelia bootstrapper then loads up user module "main"
(aurelia-app attribute on <body>) which is your src/main.js.
-->
<body aurelia-app="main">
<script src="/dist/entry-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>
{
"dependencies": {
"aurelia-bootstrapper": "^2.3.3"
}
}
<template>
<require from="./child"></require>
<h2>
Uncontrolled
</h2>
<child
child-prop.bind="childPropUncontrolled"
on-click-child.bind="onClickChild"
>Uncontrolled</child>
<h2>
Controlled
</h2>
<child
child-prop.bind="childPropControlled"
on-click-child.bind="onClickChild"
should-control.bind="false"
>Controlled</child>
</template>
export class App {
childPropUncontrolled = "Initial Parent Uncontrolled"
childPropControlled = "Initial Parent Controlled"
count = 0
onClickChild = () => {
this.count++
let count = this.count % 2 ? this.count : this.count - 1;
this.childPropControlled = "Parent Setting Controlled " + count
this.childPropUncontrolled = "Parent setting Uncontrolled " + count
console.log("Parent updated childPropA with", this.childPropA)
}
}
<template>
<p>${childProp}</p>
<button click.delegate="onClick()">Click <slot></slot></button>
</template>
import { bindable } from "aurelia-framework"
export class Child {
@bindable childProp
@bindable onClickChild
@bindable shouldControl = true
count = 0
bind() {
this.childProp = "Chaning childProp in bind()"
}
onClick() {
if (this.shouldControl) {
this.childProp = "Child Setting propA"
console.log("Child is setting propA")
} else {
this.onClickChild()
console.log("Child invoked parent prop method")
}
}
}
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging('info');
aurelia.start().then(() => aurelia.setRoot());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment