What is the use of the "Route" in the relay functions oneUrl () and allUrl ()

To sign for the oneUrl function: oneUrl(route, url)
And from the documentation:

oneUrl (route, url) . This will create a new Restangular object, which is just a pointer to a single element with the specified URL.

It seems useless to me to set Route when you specify the URL of the resource. Why does it exist in the argument list? Why is this necessary? And how can it be used?

+8
angularjs rest restangular
source share
1 answer

In my use of oneUrl I found that the route name is used to create the URL for subsequent PUT and DELETE operations. For example (pseudo code):

 // "GET /api/v2/users/3/ HTTP/1.1" 200 184 var user = Restangular.oneUrl('myuser', 'http://localhost:8000/api/v2/users/3').get(); user.name = 'Fred'; // the following uses the route name and not the URL: // "PUT /api/v2/myuser HTTP/1.1 404 100 user.put(); 

I was surprised by this behavior. I expected put() to use the same URL as get() ; which would be useful in my case.

My API uses absolute URLs in JSON payload data to go to all related resources, and I wanted to use instances of oneUrl() to GET/PUT without re-creating the routes in the JS code. But I'm pretty new to Restangular, so I may not have the mental model correctly.

+6
source share

All Articles