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

addElementTransformer/extendModel is not able to distinguish different nested resources #1289

Open
Daanoz opened this issue Jan 7, 2016 · 2 comments

Comments

@Daanoz
Copy link

Daanoz commented Jan 7, 2016

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.

@nbasov
Copy link

nbasov commented Mar 22, 2016

+1

As an quick fix you could change route case, since urls are case independent:

/* /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;
  });

@bostrom
Copy link
Collaborator

bostrom commented Oct 17, 2016

+1 for regexp matching the route name.

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

No branches or pull requests

3 participants