Optional url parameters in AngularJs Resource

angularjs, javascript

Solution

Yes, currently you need to define another $resource.

But you can wrap multiple instances of $resource into a single service if you want...

app.factory('mergedRes', function($resource) {
  var r1 = $resource('/companies/:companyId/products');
      r2 = $resource('/companies/products');

  r1.getAll = r2.query.bind(r2);

  return r1;
});

Problem

I have this kind of resource: `var Products = $resource('companies/:companyId/products')` The problem is, that I would like to get the products of all companies by url `companies/products`, but using resource and not providing companyId I am getting `companies//products`. Probably I could use another url, like just `/products`, but does it mean that I have to have another resource for the same thing? In this simple case I could change the url to `companies/products/:companyId`, but it seems to be quite general case.

Original source