Adding additional data from server to existing restangular collection
What is the right way to add more data in to an existing collection with Restanglar?
Currently on page load I am performing:
app.factory('Post', ['Restangular', function(Restangular) {
return Restangular.service('posts');
}]);
var Posts = Post.getList({embed:'comments.replies,tags', limit:'50'}).$object;
$scope.posts = Posts;
This gets my initial bunch of posts nicely.
On the front end the user can then select a tag which filters the results, and I want to load up some more from the server for the specific tag. Currently I am replacing the existing posts with:
Posts = Post.getList({embed:'comments.replies,tags', limit:'100', tag:tag.name}).$object;
$scope.posts = Posts;
The data is coming back great from the API. What is the best practice way to push the data in to the exiting/original Restangular collection?
Ideally I want to keep adding to this collection, merging the result so that there are no dupes.
Is there a Restangular method that is used to merge as it seems a common use case. (Could be used for loading additional paginated data too.)
+1 for being able to merge into a collection
@mgonto, need some best practice advice. Would greatly appreciate some feedback on how you would handle this!
Here is a first attempt:
Post.getList({embed:'comments.replies,tags', limit:'50', tags:tag.name}).then(function(postdata){
// combine the new posts with the existing posts.
var poststouniq = $scope.posts.concat(postdata.data);
// get rid of the dupes.
var uniqposts = _.uniq(poststouniq, false , function(obj){
return obj.id;
});
// update the $scope
$scope.posts = uniqposts;
});
Works well enough, but not convinced on how it will scale or of the side effects?
+1 for being able to merge into a collection