Using Self reference resources
A lot of REST APIs return the URL to self of the element that you're querying. You can use that with Restangular so that you don't have to create the URLs yourself, but use the ones provided by the server.
Let's say that when doing a GET to /people
you get the following
[{
name: "Martin",
lastName: "Gontovnikas"
self: {
link: 'http://www.example.com/people/gonto'
}
}, {
name: "John",
lastName: "Wayne"
self: {
link: 'http://www.example.com/people/jhonny'
}
}]
In this case, as you can see, the URL to each element can't be guessed so we need to use that to reference the element. Restangular supports both relative and absolute URLs :).
How do we do this with Restangular?
First, we need to configure the path for the link to self. For that, in the config we do:
RestangularProvider.setRestangularFields({
selfLink: 'self.link'
});
Then, we can just use this :)
// Instead of using all we could also use allUrl to set a URL
// Restangular.allUrl('people', 'http://www.example.com/people')
Restangular.all('people').getList().then(function(people) {
var gonto = people[0];
gonto.name = "Owned";
// This will do a PUT to http://www.example.com/people/gonto
// It uses the self linking property :D
gonto.put()
})