-
Notifications
You must be signed in to change notification settings - Fork 844
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
API enhancement: get full route for nested resource #736
Comments
I found this issue while looking for a way to do |
Just ran into this issue, too. extendModel and extendCollection are effectively useless in the scenario I'm in due to the all or nothing nature of how routes are extended. /user/detail and /customer/detail, for instance. |
+1 |
1 similar comment
+1 |
I too am running into this.
and I'd like to have all the returned photo objects extended. I tried extending it with the galleries route, but never managed to extend the photo model properly:
|
I ran into the same issue. I have customers/contacts and suppliers/contacts which are different. The next solution seems to work well :
function getFullRoute(elem) {
var currentElem = elem
var fullRoute = [];
do {
fullRoute.splice(0, 0, currentElem.route);
currentElem = currentElem.parentResource;
}
while(currentElem !== null);
return fullRoute.join('/');
}
config.transformElem = function(elem, isCollection, route, Restangular, force) {
if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
return elem;
}
var fullRoute = getFullRoute(elem);
var typeTransformers = config.transformers[fullRoute] || config.transformers[route];
var changedElem = elem;
if (typeTransformers) {
_.each(typeTransformers, function(transformer) {
changedElem = transformer(isCollection, changedElem);
});
}
return config.onElemRestangularized(changedElem,
isCollection, route, Restangular);
}; With that small changes, Restangular.extendModel('customers/contacts', function(model) {
// ...
});
Restangular.extendModel('suppliers/contacts', function(model) {
// ...
}); What do you think ? |
My solution can be found at : https://github.com/vincentdieltiens/restangular |
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? |
If I use nested resources I sometimes have to deal with generic route names. Say I have a
users
andcompanies
route and both have a nested resourceaddress
, which I can operate on withusers/:id/address
andcompanies/: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 theroute
param, because it isaddress
in both cases. I need to checkelem.parentResource.route
, too. It could look like this: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:
.getFullRoute()
would basically recursively prepend every existingparentResource.route
separated by a/
.elem.getFullRoute()
would be equal toelem.route
for every non-nested resource.What do you think?
The text was updated successfully, but these errors were encountered: