Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional url parameters in AngularJs Resource

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.

like image 790
petrov.alex Avatar asked Apr 30 '12 10:04

petrov.alex


1 Answers

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;
});
like image 74
Vojta Avatar answered Oct 21 '22 21:10

Vojta