Last active
March 26, 2016 21:39
-
-
Save arenoir/02774605f501cbc0522a to your computer and use it in GitHub Desktop.
Ember group by computed macro
This file contains 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'; | |
var get = Ember.get, | |
arrayComputed = Ember.arrayComputed; | |
export default function (dependentKey, property) { | |
var options = { | |
initialValue: [], | |
addedItem: function(array, item, changeMeta, instanceMeta) { | |
var key = get(item, property), | |
group = array.findBy('key', key); | |
if (!group) { | |
group = Ember.ArrayProxy.create({ | |
content: [], | |
key: key | |
}); | |
array.pushObject(group); | |
} | |
group.pushObject(item); | |
return array; | |
}, | |
removedItem: function(array, item, changeMeta, instanceMeta) { | |
var key = get(item, property), | |
group = array.findBy('key', key); | |
if (!group) { | |
return; | |
} | |
group.removeObject(item); | |
if (get(group, 'length') === 0) { | |
array.removeObject(group); | |
} | |
return array; | |
} | |
}; | |
return arrayComputed(dependentKey, options); | |
} |
This file contains 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'; | |
import groupBy from '../utils/group-by'; | |
export default Ember.ArrayController.extend({ | |
itemController: 'item', | |
groupedContent: groupBy('@this', 'category') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment