Skip to content

Instantly share code, notes, and snippets.

@amessinger
Last active August 4, 2020 14:09
Show Gist options
  • Save amessinger/e1ac5175bfa89b439902adf84500b27b to your computer and use it in GitHub Desktop.
Save amessinger/e1ac5175bfa89b439902adf84500b27b to your computer and use it in GitHub Desktop.
Observer
import { tracked } from '@glimmer/tracking';
import Component from '@ember/component';
import { action, computed, observer } from '@ember/object';
export default class extends Component {
updateActiveItem = observer('activeItems', function() {
console.log('updateActiveItem');
this.isActive = this.activeItems.includes(this.item);
})
@action
setActive(event) {
console.log('setActive');
event.preventDefault();
if (this.activeItems.includes(this.item)) {
this.activeItems.removeObject(this.item);
} else {
this.activeItems.pushObject(this.item);
}
console.log(this.activeItems, this.activeItems.length);
}
}
import Component from '@ember/component';
import { observer } from '@ember/object';
export default class extends Component {
updateActiveItem = observer('activeItems.[]', function() {
console.log('parent updateActiveItem');
})
}
import Controller from '@ember/controller';
import { A } from '@ember/array';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
items = A([1, 2, 3, 4]);
activeItems = A([1, 2]);
}
<ItemsContainer
@items={{this.items}}
@activeItems={{this.activeItems}}
/>
<input
type="checkbox"
checked={{this.isActive}}
{{on 'click' setActive}}
/> Item #{{@item}}
{{#if @items}}
<ul>
{{#each @items as |item|}}
<li>
<Item
@item={{item}}
@activeItems={{@activeItems}}
/>
</li>
{{/each}}
</ul>
{{else}}
<p>
No items :'(
</p>
{{/if}}
{
"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