Skip to content

Instantly share code, notes, and snippets.

@varblob
Last active October 21, 2015 07:39
Show Gist options
  • Save varblob/0233b4138b1875d947a4 to your computer and use it in GitHub Desktop.
Save varblob/0233b4138b1875d947a4 to your computer and use it in GitHub Desktop.
Example of view model wrapping normal model
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'); }),
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