Skip to content

Instantly share code, notes, and snippets.

@alexlafroscia
Last active July 13, 2020 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlafroscia/f605be2c775135cb48de4009b5f2521b to your computer and use it in GitHub Desktop.
Save alexlafroscia/f605be2c775135cb48de4009b5f2521b to your computer and use it in GitHub Desktop.
Tracking Component Arguments
import Controller from '@ember/controller';
import { tracked, TrackedObject } from 'tracked-built-ins';
import { action } from '@ember/object';
class Container {
@tracked options;
constructor(options = {}) {
this.options = options;
}
}
class Options {
@tracked foo;
constructor(foo) {
this.foo = foo;
}
}
export default class ApplicationController extends Controller {
// Option 1: Tracked binding, no tracking in object
@tracked container1 = {
options: { foo: 'bar' }
};
// Option 2: Tracked binding, tracked `options`, no tracking on `options` fields
@tracked container2 = new Container({ foo: 'bar' });
// Option 3: Tracked binding, options, and one specific field in `options`
// Note: `@tracked` appears to make the field non-enumerable...
// https://github.com/emberjs/ember.js/issues/18220
@tracked container3 = new Container(new Options('bar'));
// Option 4: Tracked binding, all recursive objects are tracked
@tracked container4 = new TrackedObject({
options: new TrackedObject({
foo: 'bar'
})
});
/** Action for using `tracked` to update object **/
@action updateContainer(container, key, { target: { value } }) {
container.options[key] = value;
}
}
<OptionsDisplay style="color: red" @container={{this.container1}} />
<input
value={{this.container1.options.foo}}
{{on 'input' (fn this.updateContainer this.container1 'foo')}}
/>
{{this.container1.options.foo}}
<OptionsDisplay style="color: blue" @container={{this.container2}} />
<input
value={{this.container2.options.foo}}
{{on 'input' (fn this.updateContainer this.container2 'foo')}}
/>
{{this.container2.options.foo}}
<OptionsDisplay style="color: green" @container={{this.container3}} />
<input
value={{this.container3.options.foo}}
{{on 'input' (fn this.updateContainer this.container3 'foo')}}
/>
{{this.container3.options.foo}}
<OptionsDisplay style="color: orange" @container={{this.container4}} />
<input
value={{this.container4.options.foo}}
{{on 'input' (fn this.updateContainer this.container4 'foo')}}
/>
{{this.container4.options.foo}}
<ul ...attributes>
{{#each-in @container.options as |key value|}}
<li>{{key}}: {{value}}</li>
{{else}}
<li>There don't appear to be any keys on this object...</li>
{{/each-in}}
</ul>
{
"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",
"tracked-built-ins": "1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment