Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested resources that can also be top level resources #139

Closed
TheLudd opened this issue Jun 24, 2013 · 3 comments
Closed

Nested resources that can also be top level resources #139

TheLudd opened this issue Jun 24, 2013 · 3 comments
Labels

Comments

@TheLudd
Copy link

TheLudd commented Jun 24, 2013

Hello.
I have a restful backend that contains companies and each company has a list of services. I get a companies list of services at the URL "company/{companyId}/servicelist"
However each service is also considered a "top level" entity. To update a service I need to send a PUT request to the address "service/{serviceId}".

If i retreive the list of services for a company with Restangular, each one of the retreived services will have an "incorrect" route. Like this:

Restangular.one("company", 42).all("servicelist").getList().then(function(serviceList) {
    var firstService = serviceList[0]; // firstService.id is 1.
    firstService.name = "A new name";
    firstService.put(); // will post to company/42/servicelist/1
});

Can I configure this so that the PUT request will be sent to "service/1"?

@mgonto
Copy link
Owner

mgonto commented Jun 24, 2013

Hey,

Thanks for using the library.

The idea is to use Nested resources for putting and posting, but you can use Restangular anyway for your case.

You should do something like this:

var firstService = serviceList[0]

var firstService = Restangular.restangularizeElement(null, firstService, 'service');

firstService.put()

Using restangularizeElement, you can restangularize any element you want, even an already Restangularized element :).

Let me know if this works out for you!

@mgonto mgonto closed this as completed Jun 24, 2013
@TheLudd
Copy link
Author

TheLudd commented Jun 25, 2013

Looks promising but when I use the the code you provided, the service still has the parentResource "company" after calling restangularizeElement with null as the first argument. Is this correct?

mgonto added a commit that referenced this issue Jun 25, 2013
@mgonto
Copy link
Owner

mgonto commented Jun 26, 2013

You're absolutely righ!

Try again with version 1.0.3. I fixed that bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants