Last active
July 13, 2020 19:20
-
-
Save alexlafroscia/f605be2c775135cb48de4009b5f2521b to your computer and use it in GitHub Desktop.
Tracking Component Arguments
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 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; | |
} | |
} |
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
{ | |
"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