Last active
August 29, 2015 13:56
-
-
Save dagda1/8802186 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
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