Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a custom method to a restangular service?

I have a decoupled Restangular service which I'd like to attach a custom method to. It appears that the only methods returned on a collection by default are getList, one, and post. I would like to do Locations.getLongLat()

I have tried adding the following to my service with no luck (the method isn't bound to the object) and I just get undefined is not an object in response.

angular.module('myApp')
.factory('Locations', function (Restangular) {

    return Restangular.withConfig(function (RestangularConfigurer) {
        RestangularConfigurer.addElementTransformer('api/v1/locations', true, function (location) {
            location.addRestangularMethod('getLongLat', 'get', 'longlat');
            return location;
        });
    }).service('api/v1/locations');
})

Anyone have any ideas?

like image 959
David Wadge Avatar asked Jun 13 '14 18:06

David Wadge


1 Answers

Just in case anyone was wondering how I resolved this - custom methods alongside a service is actually unsupported in the master branch of Restangular. Calling service will just override the collection and any custom methods applied prior to this with the defaults.

To resolve I've overrided the toService function to support custom methods.

like image 56
David Wadge Avatar answered Nov 03 '22 05:11

David Wadge