Skip to content

Instantly share code, notes, and snippets.

@Bestra
Last active September 17, 2016 11:38
Show Gist options
  • Save Bestra/7eddc1119cc517a5c83c3ed951735e3c to your computer and use it in GitHub Desktop.
Save Bestra/7eddc1119cc517a5c83c3ed951735e3c to your computer and use it in GitHub Desktop.
List with Add
// components/accordion-item.js
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'div',
item: null, // passed-in
isExpanded: Ember.computed('activeItem', 'item', function() {
return this.get('activeItem') === this.get('item');
})
});
// components/accordion-list.js
import Ember from 'ember';
export default Ember.Component.extend({
items: null,
activeItem: null
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
activeItem: null,
items: Ember.computed(function() {
return [
{title: "Item 1", details: "Details here"},
{title: "Item 2", details: "Details here"},
{title: "Item 3", details: "Details here"}
]
}),
actions: {
addItem() {
let newItem = {
title: `Item ${this.get('items.length') + 1}`,
details: 'Details here'
}
this.get('items').addObject(newItem);
this.set('activeItem', newItem);
},
toggleActiveItem(item) {
if (this.get('activeItem') !== item) {
this.set('activeItem', item);
} else {
this.set('activeItem', null);
}
}
}
});
+---------------------------------+
| |
| Parent |
| |
+---------------------------------+
||
|| items
||
||
||
_++_
\ /
\/
+------------+ (sibling) +--------------------------------+
| add|button +---------------+ accordion|list |
+------------+ +--------------------------------+
/\ ||
/__\ ||
|| || item, activeItem
|| _++_
toggleActiveItem || \ /
|| \/
|| +--------------------+
|----+ accordion+item |
+----+ |
+--------------------+
+---------------------------------+
addItem | |
+--------------------->| Parent |
| | |
| +--------------+------------------+
| ^ |
| toggleActiveItem | items, activeItem
| | v
+-----+------+ ----+-----------------------------+
| add-button | | accordion-list |
+------------+ +---+------------------+----------+
^ |
| | item, activeItem
toggleActiveItem |
| v
| +--------------------+
| | accordion-item |
+--------+ |
+--------------------+
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.header {
background: #eee;
padding: 10px;
}
.details {
padding: 10px 10px 20px 10px;
}
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{accordion-list
items=items}}
<button {{action "addItem"}}>Add Item </button>
<br>
<br>
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{accordion-list
activeItem=activeItem
items=items
toggleActiveItem=(action 'toggleActiveItem')}}
<button {{action "addItem"}}>Add Item </button>
<br>
<br>
<!-- components/accordion-item.hbs -->
<div class='header' {{action headerClicked}}>{{item.title}} (click to {{if isExpanded 'close' 'open'}})</div>
{{#if isExpanded}}
<div class='details'>{{item.details}}</div>
{{/if}}
<!-- components/accordion-list.hbs -->
<div>
{{#each items as |item|}}
{{accordion-item
item=item
activeItem=activeItem
headerClicked=(action attrs.toggleActiveItem item)}}
{{/each}}
</div>
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.7.0",
"ember-data": "2.7.0",
"ember-template-compiler": "2.7.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment