Last active
August 29, 2015 14:12
-
-
Save krambuhl/66aaa35c8b3a06b62294 to your computer and use it in GitHub Desktop.
diffCollection
This file contains hidden or 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
function diffCollection(oldColl, newColl, key) { | |
var oldVal = _.map(oldColl, function(model) { | |
return model.get(key); | |
}); | |
var newVal = _.map(newColl, function(model) { | |
return model.get(key); | |
}); | |
var changed = []; | |
var removed = _.chain(oldColl).map(function(model) { | |
if (_.indexOf(newVal, model.get(key)) === -1) { | |
return model; | |
} else { | |
changed.push(model); | |
} | |
}).unique().value(); | |
var add = _.filter(newColl, function(model) { | |
return _.indexOf(oldVal, model.get(key)) === -1; | |
}); | |
return { | |
add: add, | |
removed: removed, | |
changed: changed | |
}; | |
} |
Author
krambuhl
commented
Dec 27, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment