Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .all() and .one() in Restangular?

What is the difference between these two? Both seems to make a GET to /users and retrieve them.

Restangular.one('users').getList().then(function(users) {
    // do something with users
});

Restangular.all('users').getList().then(function(users) {
    // do something with users
});

I understand that you can do one('users', 123) and it will retrieve /users/123 but without the second argument it seems to be the same thing. Why not just have one method in that case?

like image 738
user2906759 Avatar asked Mar 20 '14 11:03

user2906759


2 Answers

The one() function has a second argument that accepts an id e.g. .one('users', 1).

  • one('users', 1).get() translates to /users/1
  • all('users').getList() translates to /users

Unlike all(), one() is not generally used with .getList() without argument. However, if you were to call .one('users', 1).getList('emails') or .one('users', 1).all('emails').getList(), then you would make a GET request to /users/1/emails.

like image 83
lyschoening Avatar answered Nov 14 '22 20:11

lyschoening


My guess is that they are there for expressing an intention of what you are going to do. I would understand those as a way to build the url, expressing if you are accessing to the whole resource or to a specific one.

In the end, they are gonna build and do a GET request but because you do a GET and retrieve some data it does not mean that it should be used in that way.

Example extracted from https://github.com/mgonto/restangular/issues/450

getList can be called both ways. If it's called in an element one, then it needs a subelement to get to a Collection. Otherwise, it fetches the collection. So the following is the same:

Restangular.one('places', 123).getList('venues')        // GET /places/123/venues
Restangular.one('places', 123).all('venues').getList()  // GET /places/123/venues

As you can see, it is more expressive to call one('places', 123).all('venues') to understand that you just want the venues located in the area/place 123.

Maybe the following url will help you:

https://github.com/mgonto/restangular/issues/450

like image 26
kitimenpolku Avatar answered Nov 14 '22 20:11

kitimenpolku