Skip to content

Instantly share code, notes, and snippets.

@mauritslamers
Forked from dcporter/gist:6536417
Last active December 30, 2015 10:09
Show Gist options
  • Save mauritslamers/7813738 to your computer and use it in GitHub Desktop.
Save mauritslamers/7813738 to your computer and use it in GitHub Desktop.
// need to add destroy of internal cache on destroy...
SC.ManyArray.reopen({
// Set up observers.
contentDidChange: function() {
var observedRecords = this._observedRecords;
if (!observedRecords) observedRecords = this._observedRecords = [];
var record, i, len;
// If any items in observedRecords are not in content, stop observing them.
len = observedRecords.length;
for (i = len - 1; i >= 0; i--) {
record = observedRecords.objectAt(i);
if (!this.contains(record)) {
record.removeObserver('status', this, this.notifyStatusChange);
observedRecords.removeObject(record);
}
}
// If any item in content is not in observedRecords, observe them.
len = this.get('length');
for (i = 0; i < len; i++) {
record = this.objectAt(i);
if (!observedRecords.contains(record)) {
record.addObserver('status', this, this.notifyStatusChange);
this.invokeOnce(this.notifyStatusChange);
observedRecords.pushObject(record);
}
}
}.observes('[]'),
notifyStatusChange: function() {
this.notifyPropertyChange('status');
},
// Numerical order is a reasonable proxy for status hierarchy - most bad statuses are higher than most good statuses.
// TODO: ...actually order the statuses
status: function() {
var length = this.get('length');
var maxStatus = 0;
for (i = 0; i < length; i++) {
var status = this.objectAt(i).get('status');
maxStatus = status > maxStatus ? status : maxStatus;
}
return maxStatus || SC.Record.EMPTY;
}.property().cacheable()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment