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?


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.

链接地址: http://www.djcxy.com/p/20934.html

上一篇: Jquery Scroller需要在到达结束后停止滚动

下一篇: 如何将自定义方法添加到restangular服务?