Skip to content

Instantly share code, notes, and snippets.

@theoldcounty
Last active October 1, 2018 23:00
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 theoldcounty/2ba652b74684ce31238c7409c4720af5 to your computer and use it in GitHub Desktop.
Save theoldcounty/2ba652b74684ce31238c7409c4720af5 to your computer and use it in GitHub Desktop.
Parent / Child Component Communication
import Component from '@ember/component';
import { action, computed, observes } from '@ember-decorators/object';
export default class extends Component {
selectedItems = [];
_cbState = false;
_changeCounter = 0;
parent = false;
@observes('changeCounter')
watchForChanges() {
if (!this.parent) return;
if (this._changeCounter === this.changeCounter) return;
this.set('_changeCounter', this.changeCounter);
// this.set('cbState', false);
}
@computed('parentValue')
get cbState() {
console.log(this.parentValue, this._cbState);
if (this.parentValue) {
this._cbState = false;
}
return this._cbState;
}
set cbState(value) {
this._cbState = value;
}
@action
myChangeAction(nextValue) {
this.set('cbState', nextValue);
//console.log("type", this);
console.log(this.group)
if (nextValue) {
this.get('selectedItems').pushObject(this);
} else {
this.get('selectedItems').removeObject(this);
}
console.log("this.get('selectedItems')", this.get('selectedItems'))
if (this.trackChange) {
this.trackChange();
}
}
}
import Component from '@ember/component';
import { action, computed } from '@ember-decorators/object';
export default class extends Component {
changeCounter = 0;
@action
trackChange() {
this.set('changeCounter', this.changeCounter + 1);
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
items : [{
"title" : "burger",
"id": "bk2",
"subgroup" : [
{"name" : "tomato", "id": "tomo2"}, {"name" : "lettuce", "id": "let2"}, {"name" : "pickle", "id": "pickl3"}]
},
{
"title" : "kebab",
"id": "kb2",
"subgroup" : [{"name" : "ketchup", "id": "ketchu2"}, {"name" : "chilli", "id": "chilli2"}]
}]
});
import Ember from 'ember';
export function not(params/*, hash*/) {
return !params[0];
}
export default Ember.Helper.helper(not);
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.checkboxhandler{
position: relative;
margin-left: 10px;
margin-bottom:10px;
border: 1px solid red;
}
<h1>Welcome to {{appName}}</h1>
<br>
<WrappingComponent as |changeCounter trackChange|>
<label>changeCounter: {{changeCounter}}</label>
{{#each items as |item index|}}
<CheckboxComponent
@label={{item.title}}
@item={{item}}
@group={{index}}
as |parentValue changeParent|
>
{{#each item.subgroup as |itm i|}}
<CheckboxComponent
@label={{itm.name}}
@item={{itm}}
@group={{index}}
@parentValue={{parentValue}}
>
</CheckboxComponent>
{{/each}}
</CheckboxComponent>
{{/each}}
</WrappingComponent>
<br>
{{outlet}}
<br>
<br>
<div class="checkboxhandler">
<input
type="checkbox"
checked={{this.cbState}}
id="click-drag-item-{{this.item.id}}"
onclick={{action this.myChangeAction (not this.cbState) this.group }}>
<label>{{@label}}</label>
<label class='info'>cbState: {{this.cbState}}</label>
{{yield this.cbState (action this.myChangeAction)}}
</div>
{{yield this.changeCounter (action this.trackChange)}}
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.1",
"ember-template-compiler": "3.4.1",
"ember-testing": "3.2.2"
},
"addons": {
"ember-decorators": "2.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment