Skip to content

Instantly share code, notes, and snippets.

@dagda1

dagda1/group.js

Created Feb 4, 2014
Embed
What would you like to do?
App.computed.groupable = function(dependentKey, groupBy){
var options = {
initialValue: [] ,
initialize: function(array, changeMeta, instanceMeta){
},
addedItem: function(array, item, changeMeta, instanceMeta){
var key = groupBy(item);
var group = array.findBy('key', key);
if(!group){
group = Ember.Object.create({key: key, count: 0});
array.pushObject(group);
}
group.incrementProperty('count');
return array;
},
removedItem: function(array, item, changeMeta, instanceMeta){
var key = groupBy(key);
var group = array.findBy('key', key);
if(!group){
return;
}
var count = group.decrementProperty('count');
if(count === 0){
array.removeObject(group);
}
return array;
}
};
return Ember.arrayComputed(dependentKey, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.