Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple routing urls for single service AngularJS

Tags:

rest

angularjs

I have multiple URL paths that I would like to map to a single resource. However I am unsure how to change the URL based on the function called. For example the :dest mapping for query would be /allProducts, however destroy would be something along the lines of /delete/:id

service.factory('ProductsRest', ['$resource', function ($resource) {
    return $resource('service/products/:dest', {}, {
        query: {method: 'GET', params: {}, isArray: true },
        save: {method: 'POST'},
        show: { method: 'GET'},
        edit: { method: 'GET'},
        update: { method: 'PUT'},
        destroy: { method: 'DELETE' }
    });
}]);
like image 247
zmanc Avatar asked Mar 01 '13 15:03

zmanc


2 Answers

For each action you can override the url argument. Specially for this is the url: {...} argument.

In your example:

service.factory('ProductsRest', ['$resource', function ($resource) {
    return $resource('service/products/', {}, {
        query: {method: 'GET', params: {}, isArray: true },
        save: {method: 'POST', url: 'service/products/modifyProduct'},
        update: { method: 'PUT', url: 'service/products/modifyProduct'}
    });
}]);
like image 177
mikemueller Avatar answered Oct 22 '22 08:10

mikemueller


I just needed to put the url in as the param.

service.factory('ProductsRest', ['$resource', function ($resource) {
    return $resource('service/products/:dest', {}, {
        query: {method: 'GET', params: {dest:"allProducts"}, isArray: true },
        save: {method: 'POST', params: {dest:"modifyProduct"}},
        update: { method: 'POST', params: {dest:"modifyProduct"}},
    });
}]);
like image 43
zmanc Avatar answered Oct 22 '22 10:10

zmanc