Skip to content

Instantly share code, notes, and snippets.

@varblob
Forked from HenryVonfire/README_01.md
Last active October 20, 2015 22:04
Show Gist options
  • Save varblob/2918aa61e1f13a9627cb to your computer and use it in GitHub Desktop.
Save varblob/2918aa61e1f13a9627cb to your computer and use it in GitHub Desktop.
Expandable list
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Expandable list',
buttonName: 'expand all',
families: [{title:'parent a',
children: [
{ title:'a son' },{ title:'a daughter' }
]
},
{title:'parent b',
children: [
{ title:'b son' }
]
},
{ title:'single c' }
],
familyModels: Ember.computed('families', function(){
return this.get('families').map((p)=>{
return Ember.Object.create({
isExpanded: false,
parent:p
});
});
}),
expandedModels: Ember.computed.filterBy('familyModels', 'isExpanded', true),
allExpanded: Ember.computed('expandedModels.length', function(){ return this.get('expandedModels.length') === this.get('familyModels.length'); }),
setAllFamilyModels: function(isExpanded){
},
actions:{
toggleAllExpanded:function(){
var expand = !this.get('allExpanded');
this.get('familyModels').forEach((parent)=> parent.set('isExpanded', expand));
}
}
});
<h1>{{appName}}</h1>
<p>Click on the parents to see their children</p>
<br>
<button {{action 'toggleAllExpanded'}}>{{if allExpanded 'collapse all' 'expand all'}}</button>
<br>
<br>
{{#each familyModels as |model|}}
{{item-list model=model expandAll=expandList}}
{{/each}}
<br>
<br>
import Ember from 'ember';
export default Ember.Component.extend({
model: null,
parent: Ember.computed.alias('model.parent'),
isExpanded: Ember.computed.alias('model.isExpanded'),
click: function(){
this.toggleProperty('isExpanded');
}
});
{{parent.title}}<br>
{{#if isExpanded}}
<ul>
{{#each parent.children as |child|}}
<li>{{child.title}}</li>
{{/each}}
</ul>
{{/if}}
{
"version": "0.4.13",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.10/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.13/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.10/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment