Skip to content

Instantly share code, notes, and snippets.

@dagda1
Created February 26, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagda1/9234529 to your computer and use it in GitHub Desktop.
Save dagda1/9234529 to your computer and use it in GitHub Desktop.
// USAGE
// App.SomeModel = DS.Model.extend ({
// users: DS.hasMany('users'),
// contacts: DS.hasMany('contacts')
// // Aggregate users and contacts into one array
// people: App.computed.aggregate('users', 'contacts')
// })
//
App.computed.aggregate = function() {
var args, options, properties;
properties = Array.prototype.slice.call(arguments);
args = properties.map((function(_this) {
return function(prop) {
return "" + prop;
};
})(this));
options = {
initialValue: [],
addedItem: function(array, item, changeMeta, instanceMeta) {
var observer;
if (array.contains(item)) {
return array;
}
observer = (function(_this) {
return function() {
if (!item.get('isLoaded')) {
return;
}
item.removeObserver('isLoaded', observer);
return array.addObject(item);
};
})(this);
if (item.get('isLoaded')) {
observer();
} else {
item.addObserver('isLoaded', observer);
}
return array;
},
removedItem: function(array, item, changeMeta, instanceMeta) {
if (!array.length) {
return;
}
if (!array.contains(item)) {
return;
}
array.removeObject(item);
return array;
}
};
args.push(options);
return Ember.arrayComputed.apply(this, args);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment