Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have currently been building a CRUD application in a MEAN stack. But I have encountered a potential issue I cannot seem to solve.

A brief overview is I have a number of blog posts and want basic pagination to load more posts on an ng-click event (based off the _id) I can fetch the data but im having an issue updating the $scope with the newly fetched objects.

Load More Posts HTML:

<div ng-click="loadMoreBlogPosts(post._id)" ng-show="$last">Load more</div>

Within my Angular Controller:

// All Blog Posts (This works fine)
$scope.allPosts = function() {
    $scope.posts = Blog.Posts.query();
}

// Load More Blog Posts 
$scope.loadMorePosts = function(id) {
    /* Current Issue */
    Blog.MorePosts.query({id:id}, function(data) { 
       $scope.posts.push(data) /* This doesn't work */
    })
}

Angular Model:

return {

        Posts: $resource('/api/blog', {}, { 

            query: { method: 'GET', isArray: true },
            add: { method:'POST', isArray:false },

        }),

        MorePosts : $resource('/api/moreposts/:id', {}, { 

            query: { method: 'GET', isArray: true, params:{ id:'@id' } }

        }),

        Post: $resource('/api/blog/post/:id', {}, { 

            query: { method: 'GET', isArray: true, params:{ id:'@id' } },
            add: { method:'POST', isArray:false },
            update: { method:'PUT', isArray:false, params:{ id:'@id' } },
            delete: { method:'DELETE', params:{ id:'@id' } }

        }),
} 

Mongoose/MongoDB/Node Controller:

exports.posts = function(req, res, next) {

    // Blog Posts
    Blog.find()
        .sort('-created')
        .limit(3)
        .find(function (err, posts) {
        // Check
        if(err) {
            // Error
            return next(err)
        } else {
            // Success
            res.json(posts) 
        }
    })

};


exports.moreposts = function(req, res, next) {

    //console.log(req.params.id);
    // Blog Posts
    Blog.find({"_id": { "$lt": req.params.id }})
        .sort('-created')
        .limit(2)
        .find(function (err, posts) {
        // Check
        if(err) {
            // Error
            return next(err)
        } else {

            // Success
            res.json(posts) 
            console.log(posts);/* Showing in Terminal */

        }
    })

};

This has been perplexing me for a few hours now. Im certain I must be overlooking something rather simple, or that in-fact my logic is entirely incorrect (I hope its the former) Any help or guidance would be appreciated.

On a side note, if the loadMoreBlogPosts function is fired, with the following:

$scope.posts = Blog.MorePosts.query({id:id})

The above will show change the $scope.posts to show the objects returned from the Node/mongo controller. This shows that the logic is fine, I just still need to find a way to append the data to $scope.posts, not replace it.

share|improve this question
up vote 2 down vote accepted

The following:

$scope.posts.push(data);

Would add the new array to the end of the existing one, making it a nested array.

What you want is to merge the old array with the new array, without replacing the old one.

The following should work:

Blog.MorePosts.query({id:id}, function(data) { 
  $scope.posts.push.apply($scope.posts, data);
});

If you are using track by, for example:

<div ng-repeat="post in posts track by post.id">

Then it will not matter if you recreate the original array and can then use concat:

Blog.MorePosts.query({id:id}, function(data) { 
  $scope.posts = $scope.posts.concat(data);
});
share|improve this answer
    
You my friend just beat me to the punch, I was just about to test this (thats what they all say ;) ) I was thinking of going down the route of push.apply or possibly .concat. The latter didn't work, but this did. Cheers Buddy. – HireLee Jan 4 '15 at 21:08
    
You're welcome :) I just edited my answer as you responded. concat should work as long as you use the new array it returns. If you are not using track by however the concat alternative will make ng-repeat rerender the entire collection. – tasseKATT Jan 4 '15 at 21:12
2  
@HireLee You could use the voting system as well apart from accepting the answers for your questions, just so the answer does not remain zero score accepted answer.. :) see UpVote section 1) If an answer has helped you solve your problem and you accept it you should also consider voting it up. 2) If other answers have been helpful too, you can also vote them up. – PSL Jan 5 '15 at 19:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.