[Core] Aggregate models more intelligently

When aggregating models from multiple providers, prefer the
more recent version, based on its modified timestamp. WTD-1033.
This commit is contained in:
Victor Woeltjen 2015-03-20 16:13:14 -07:00
parent f9b8b17ff6
commit a640af6bf9

View File

@ -18,6 +18,19 @@ define(
*/
function ModelAggregator($q, providers) {
// Pick a domain object model to use, favoring the one
// with the most recent timestamp
function pick(a, b) {
if (!a) {
return b;
}
if (!b) {
return b;
}
return (a.modified || Number.NEGATIVE_INFINITY) >
(b.modified || Number.NEGATIVE_INFINITY) ? a : b;
}
// Merge results from multiple providers into one
// large result object.
function mergeModels(provided, ids) {
@ -25,7 +38,7 @@ define(
ids.forEach(function (id) {
provided.forEach(function (models) {
if (models[id]) {
result[id] = models[id];
result[id] = pick(result[id], models[id]);
}
});
});