Skip to content

Instantly share code, notes, and snippets.

@krambuhl
Last active August 29, 2015 14:12
Show Gist options
  • Save krambuhl/66aaa35c8b3a06b62294 to your computer and use it in GitHub Desktop.
Save krambuhl/66aaa35c8b3a06b62294 to your computer and use it in GitHub Desktop.
diffCollection
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
};
}
@krambuhl
Copy link
Author

var collection = new MyCollection();
var currentData = [], lastData;

collection.on('reset', function(coll) {
  lastData = currentData;
  currentData = coll.models;

  var diff = diffCollection(lastData, currentData, 'hash');
  // diff data
  // update dem models
});

setInterval(function() {
  collection.fetch({ reset: true });
}, 10 * 1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment