Skip to content

Instantly share code, notes, and snippets.

@luxzeitlos
Created May 25, 2020 06:08
Show Gist options
  • Save luxzeitlos/160421aaa16a66e7f90f99783d7ddcb7 to your computer and use it in GitHub Desktop.
Save luxzeitlos/160421aaa16a66e7f90f99783d7ddcb7 to your computer and use it in GitHub Desktop.
New Twiddle
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import {action} from '@ember/object';
export default class extends Component {
}
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import {action} from '@ember/object';
export default class extends Component {
data = [{ id: 1 }, {id: 2},{id: 3}];
@tracked expanded = [];
@action toggleOne(id) {
if(this.expanded.includes(id)) {
this.expanded = this.expanded.filter(x => x !== id);
} else {
this.expanded = [...this.expanded, id];
}
}
@action toggleAll() {
if(this.expanded.length === 0) {
this.expanded = this.data.map(e => e.id);
} else {
this.expanded = [];
}
}
}
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
import { helper } from '@ember/component/helper';
export default helper(function contains([elem, arr]/*, hash*/) {
return arr.includes(elem);
});
<div>
<button {{on "click" @toggle}}>Toggle from Box</button>
{{#if @expanded}}
if expanded
{{else}}
is hidden
{{/if}}
</div>
Debug expanded: {{this.expanded}}
<button {{on "click" this.toggleAll}}>Toggle all</button>
{{#each this.data as |item|}}
<Box @expanded={{contains item.id this.expanded}} @toggle={{fn this.toggleOne item.id}} />
{{/each}}
{
"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