API enhancement: get full route for nested resource
See original GitHub issueIf I use nested resources I sometimes have to deal with generic route names. Say I have a users
and companies
route and both have a nested resource address
, which I can operate on with users/:id/address
and companies/:id/address
.
Now I want to enhance the restangularized element with custom logic inside Restangular.setOnElemRestangularized
, but I need to treat the address of a user differently than the address of a company. I can’t just look into the route
param, because it is address
in both cases. I need to check elem.parentResource.route
, too. It could look like this:
Restangular.setOnElemRestangularized(function(elem, isCollection, route) {
if(elem.parentResource.route + '/' + route === 'companies/address') { /* do stuff */ }
if(elem.parentResource.route + '/' + route === 'users/address') { /* do stuff */ }
});
This can become quite verbose and error prone. It becomes even worse with deeper nested resources or in more generic situations where I have to make sanity checks (e.g. is elem.parentResource
defined?).
It would be very useful to get the full route of a resource like this:
Restangular.setOnElemRestangularized(function(elem, isCollection, route) {
if(elem.getFullRoute() === 'companies/address') { /* do stuff */ }
if(elem.getFullRoute() === 'users/address') { /* do stuff */ }
});
.getFullRoute()
would basically recursively prepend every existing parentResource.route
separated by a /
. elem.getFullRoute()
would be equal to elem.route
for every non-nested resource.
function getFullRoute(elem) {
var parentRoute = '';
if(elem.parentResource) {
parentRoute = getFullRoute(elem.parentResource) + '/';
}
return parentRoute + elem.route;
}
What do you think?
Issue Analytics
- State:
- Created 9 years ago
- Comments:8
Top GitHub Comments
I ran into the same issue. I have customers/contacts and suppliers/contacts which are different.
The next solution seems to work well :
With that small changes,
extendModel
will work like this :What do you think ?
This issue is still open and still seems relevant. Is there a workaround or a fix? How can I apply custom methods to nested model objects?