Search code examples
javascriptangularjsrestangularngresource

Restangular - How to get the request's plain results (not wrapped)


I am trying to get some data from a REST api using mgonto's Restangular.

// Restangular returns promises
Restangular.all('users').getList()  // GET: /users
.then(function(users) {
  // returns a list of users
  $scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})

// Later in the code...

// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars');  // GET: /users/123/cars

That's fine, and this works, but all the results returned in the promises callbacks have some methods and properties added by Restangular (this is how you can do .getList("cars") on a user).

What I want is to retrieve only the user's data (name, id...) without all the Restangular methods. Just a plain JS object.

I couldn't find any way to do this in the docs. Everytime I use a method on a returned user it always returns a wrapped object with the Restangular methods.


Solution

  • I guess you are looking for 'plain()' (alias for Restangular.stripRestangular(elem)).

    plain(): Returns the plain element received from the server without any of the enhanced methods from Restangular. It's an alias to calling Restangular.stripRestangular(elem)

    It strips all the restangular methods and returns the plain object which is returned by the server.

    For more information please refer to the following link :

    https://github.com/mgonto/restangular#element-methods

    The following fiddle might help :

    Fiddle ::http://plnkr.co/edit/oMFnYM4HkaFK3biscpTo?p=preview