mercredi 30 décembre 2015

How to force Restangular's getList to work with Laravel 5's pagination object?

I have a page where I want to display user's posts paginated. For back-end I use Laravel's resource controllers and on the front-end Restangular services.

So back-end implementation looks like this:

// url is /users/0/posts
public function index(Post $post)
{
    return $post->whereUserId(\Auth::id())->paginate(20);
}

Front-end looks like this:

/*
 * posts.service.js
 */
angular.module('app.services')
    .service('posts', Posts);

Posts.$inject = ['Restangular'];

function Posts(Restangular) {
    return Restangular.service('posts', Restangular.one('users', 0));
}



/*
 * posts.controller.js
 */
angular.module('koodzo.controllers')
    .controller('PostsController', PostsController);

ContentController.$inject = ['posts'];

function PostController(posts) {
    var vm = this;

    vm.posts = posts.getList()
        .then(function(response) {
            vm.pagination = response;
            vm.posts = response.data;
        });
}

So, obviously, this doesn't work, because Restangular's getList expects to receive an array from the server to restangularize it, but Laravel's paginator returns an object and the array is in the data field of the object.

The question is how to make Restangular see that response.data is the array that it needs?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire