Skip to content

Instantly share code, notes, and snippets.

@zefei
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zefei/db15affe9848ef00cd67 to your computer and use it in GitHub Desktop.
Save zefei/db15affe9848ef00cd67 to your computer and use it in GitHub Desktop.
Using Angular and Meteor together: improved collection updates
// compare newArray and oldArray, copy unchanged oldArray items over to
// newArray, hence bypass Angular dirty check for these items
function updateCollection(newArray, oldArray) {
if (!newArray || !oldArray) return newArray;
for (var i = 0; i < newArray.length; i++) {
for (var j = 0; j < oldArray.length; j++) {
if (angular.equals(newArray[i], oldArray[j])) {
newArray[i] = oldArray[j];
break;
}
}
}
return newArray;
}
// updated data binding using updateCollection
autorun($scope, function() {
Meteor.subscribe('myMeteorCollection');
// $scope.myModel = MyMeteorCollection.find().fetch();
$scope.myModel = updateCollection(MyMeteorCollection.find().fetch(), $scope.myModel);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment