Last active
October 21, 2015 07:39
-
-
Save varblob/0233b4138b1875d947a4 to your computer and use it in GitHub Desktop.
Example of view model wrapping normal model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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