Created
February 26, 2014 17:42
-
-
Save dagda1/9234529 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
// 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