Skip to content

Instantly share code, notes, and snippets.

@amk221
Last active June 30, 2023 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amk221/7d1ea0b2dee1ba5bb93850e5a111c5c4 to your computer and use it in GitHub Desktop.
Save amk221/7d1ea0b2dee1ba5bb93850e5a111c5c4 to your computer and use it in GitHub Desktop.
didUpdateAttrs replacement
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class extends Component {
@tracked value;
constructor() {
super(...arguments);
this.value = this.args.value;
}
@action
changeValue() {
this.value++;
this.args.onChangeValue(this.value);
}
@action
handleChangedArguments() {
// Component can't respond to a controller giving it a new value :(
// (without using a modifier!?)
this.value = this.args.value;
}
}
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class ApplicationController extends Controller {
@tracked value = 1;
@action
changeValue() {
this.value++;
}
@action
handleChangeValue(value) {
// Controller can respond to component giving it a new value
// but not vice versa :(
this.value = value;
}
}
.my-component {
border: 1px solid red;
padding: 10px;
margin: 10px;
}
<p>
State is intentionally stored inside both the component and the controller.
The component can change the value by itself.
Should the controller give the component a new value
(by clicking "Change value outside component")
then the component should be able to read that in.
This used to be possible with didUpdateAttrs.
</p>
External value: {{this.value}}<br />
<br />
<button type="button" {{on "click" this.changeValue}}>
Change value outside component
</button>
<MyComponent
@value={{this.value}}
@onChangeValue={{this.handleChangeValue}}
/>
<div class="my-component" {{!did-update @value}}>
Internal value: {{this.value}}<br />
<br />
<button type="button" {{on "click" this.changeValue}}>
Change value inside component
</button>
</div>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment