简体   繁体   English

如何将自定义方法添加到restangular服务?

[英]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. 我有一个解耦的Restangular服务,我想附加一个自定义方法。 It appears that the only methods returned on a collection by default are getList , one , and post . 看来默认情况下集合上返回的唯一方法是getListonepost I would like to do Locations.getLongLat() 我想做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. 我已经尝试将以下内容添加到我的服务中而没有运气(该方法未绑定到该对象)并且我只是得到undefined is not an object响应中undefined is not an object

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. 万一有人想知道我是如何解决这个问题的 - 在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. 要解决我已经覆盖了toService函数以支持自定义方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM