Created
February 4, 2014 11:17
-
-
Save dagda1/8801886 to your computer and use it in GitHub Desktop.
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
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