Skip to content

Instantly share code, notes, and snippets.

@dcporter
Created September 12, 2013 12:15
Show Gist options
  • Save dcporter/6536417 to your computer and use it in GitHub Desktop.
Save dcporter/6536417 to your computer and use it in GitHub Desktop.
Monkeypatches ManyArray with live status.
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