question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ignore parentResource when restangularizing object with option to include

See original GitHub issue

Consider:

// GET /accounts/5/mails
var mails = Restangular.one('accounts', 5).getList('mails');

// Logs: /accounts/5/mails/1
console.log(mails[0].getRestangularUrl());

// PUT: /accounts/5/mails/1
mails[0].body = 'Changed Body of Mail';
mails[0].put();

A lot of RESTful best practices (http://info.apigee.com/Portals/62317/docs/web api.pdf) suggest “Remember that once you have the primary key for one level, you usually don’t need to include the levels above because you’ve already got your specific object. In other words, you shouldn’t need too many cases where a URL is deeper than /resource/identifier/resource”.

For the above example code and taking this best practice to heart it would be better for the getRestangularUrl and PUT/DELETE etc operations to work as follows:

// GET /accounts/5/mails
var mails = Restangular.one('accounts', 5).getList('mails');

// Logs: /mails/1
console.log(mails[0].getRestangularUrl());

// PUT: /mails/1
mails[0].body = 'Changed Body of Mail';
mails[0].put();

So that the children of the restangularCollection point to their base object url unless we optionally specify to include it’s parent in the URL eg: mails[0].withParents().getRestangularUrl(), mails[0].withParents().put() perhaps?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
rynzcommented, Jul 10, 2013

@mgonto Ah, there is one bug I found with it.

// Does not work, GET /mails is the wrong request.
Restangular.one('accounts', 5).all('mails').getList();
// Works, GET /accounts/5/mails is the correct request.
Restangular.one('accounts', 5).getList('mails');
0reactions
mgontocommented, Jul 12, 2013

I’ve been thinking on adding URL Building to this feature, but all I’ve comed up with is kind of hacky, so I think that for now, I won’t leave URL Building in. IF you want to build a URL, you can set those resources as “parentful” and that’s it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found