Having a better understanding of one, all, get, and getList
See original GitHub issueHi there,
Can anyone lend a hand, I have a tough time understanding the difference between one,all,get and getList. I have a working product but I don’t think I am using it correctly so I decided to refactor it and then came to the conclusion that i really don’t understand when to use one over the other.
Here is what i understand
all > point to a collection ex var cars = Restangular.all(‘Stock/Cars’);
so if this points to a collection i presume I can do a getList() ? But getList() needs a sub element.
I have been doing a get() here as i need to pass in some querystring params and getListyou have to pass in a sub element which I don’t have.
the other thing I find confusing is the “one”, it states that one points just to one element, actually I get a list of Cars that are ford by doing the following
Restangular.one(‘Stock/Cars’, carType).get({colour: ‘red’}).then
As you can see I have built up a /Stock/Cars/Ford?colour=red (carType=Ford)
In the docs it states
one(route, id): This will create a new Restangular object that is just a pointer to one element with the route route and the specified id.
I have used “one” to build up my call but it certainly doesn’t point to one element, I get a number of elements back.
I couldn’t seem to use the getList here as I had to pass in the sub element which i don’t have. so I used “get” which allows me to pass in some queryString params.
It states in the documentation
get([queryParams, headers]): Gets the element.
which sounds like it just 1 element when in fact i get over 20 cars returned, so its not just element.
I hope somebody can help me here.
Issue Analytics
- State:
- Created 10 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
So, it does work as it does the HTTP request. The difference is that all queries done with
get
andone
expect an element, so I’m not restangularizing all elements.When you call getList, I restangularize the list itself and then each of the elements. Getting a list with get works, but you won’t get the elements restangularized, and therefore, you won’t be able to call put or get on them.
I would anyway add the stock to the baseUrl. If you need to call it everytime, then set it to the base.
Your welcome.I thinks its always a good idea to get other peoples opinions too.