Skip to content

Instantly share code, notes, and snippets.

@dagda1

dagda1/grouped2.js

Last active Aug 29, 2015
Embed
What would you like to do?
groupedDeals: Ember.arrayComputed('contacts', 'deals.@each.status', {
initialValue: [],
initialize: function(array, changeMeta, instanceMeta) {
array.pushObject(Ember.Object.create({key: 'open',name: 'Open Deals',count: 0,value: 0,deals: Ember.A()}));
array.pushObject(Ember.Object.create({key: 'closed',name: 'Closed Deals',count: 0,value: 0,deals: Ember.A()}));
array.pushObject(Ember.Object.create({key: 'lost',name: 'Lost Deals',count: 0,value: 0,deals: Ember.A()}));
},
addedItem: function(array, deal, changeMeta, instanceMeta) {
var contact = deal.get('contact');
var observer = (function(_this) {
return function() {
var group, status;
if (!contact.get('isLoaded')) {
return;
}
contact.removeObserver('isLoaded', observer);
if (contact.get('company') !== _this.get('model')) {
return;
}
status = deal.get('status');
group = ['closed', 'lost'].contains(status) ? array.findBy('key', status) : array.findBy('key', 'open');
group.incrementProperty('count');
group.incrementProperty('value', deal.get('value'));
group.get('deals').pushObject(deal);
};
})(this);
if (!contact) {
return;
}
if (!contact.get('isLoaded')) {
contact.addObserver('isLoaded', observer);
} else {
observer();
}
return array;
},
removedItem: function(array, deal, changeMeta, instanceMeta) {
var group;
group = array.find(function(group) {
return group.get('deals').contains(deal);
});
if (!group) {
return;
}
group.decrementProperty('count');
group.decrementProperty('value', deal.get('value'));
group.get('deals').removeObject(deal);
return array;
}
}),
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.