-
Notifications
You must be signed in to change notification settings - Fork 835
Description
In our current setup we use extendModel to add additional functionality to the returned restangular objects.
Our problem is that the typeTransformers registery is a simple key/value storage. So route X has transformer Y.
Example, say we have to consume an api:
/api/wheels (Endpoint containing all types of wheels we have)
/api/cars (Endpoint containing all cars we have)
/api/cars/21EC2020/wheels (Endpoint containing the (4) wheels of the car with the id of 21EC2020)
So the wheel types collection has quite some different functions as the wheels collection on the car, so when we try to map this with extend model, we'll end up with something in line of this:
/* /api/wheels */
Restangular.extendModel('wheels', function(model) {
model.swapTireType = function() {};
return model;
});
/* /api/cars/<car-id>/wheels */
Restangular.extendModel('wheels', function(model) {
model.detachFromCar = function() {};
return model;
});
Currently this will bind the detachFromCar function to all objects returned from both api's (and the swapTireType function is never bound)... Of course you could say, change the api route to wheelTypes instead of wheels, but that's not always an option.
My suggestion would be that the extendModel (/addElementTransformer) would also be to determine the path to model being extended. The way I see this, this can be done 2 ways:
- Support the use of a regex as the route param:
/* /api/cars/<car-id>/wheels */
Restangular.extendModel(/.*\/cars\/[a-z0-9]*\/wheels/ig, function(model) {
model.detachFromCar = function() {};
return model;
});
- Support defining the parent as a parameter
/* /api/cars/<car-id>/wheels */
var cars = Restangular.all('cars')
Restangular.extendModel('wheels', cars, function(model) {
model.detachFromCar = function() {};
return model;
});
So, what are your thoughts? Is there another way, if not, what would be a potential solution? I might be able to make a change and create a pull request if we can decide on a solution.