Skip to content

Instantly share code, notes, and snippets.

@dagda1

dagda1/aggregate.js

Created Feb 26, 2014
Embed
What would you like to do?
// 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
You can’t perform that action at this time.