Typescript,$ resource和自定义描述符

时间:2016-07-14 19:49:41

标签: angularjs typescript

我有一个dataAccessService类,我想在我的类中添加自定义操作(副本)。我怎么能添加,所以打字稿可以调用dataAccessService.copy()?我查看了Extending $resource of angularjs using IResourceClass of typescript,但它使用的是工厂,我想使用服务

module rebateMaintenance.common {

interface IDataAccessService {
    getRebateResource(): ng.resource.IResourceClass<IRebateResource> ;
}

interface IRebateResource
    extends ng.resource.IResource<rebateMaintenance.domain.IRebate>{
}

export class DataAcessService
    implements IDataAccessService {

    static $inject = ["$resource"];
    constructor(private $resource: ng.resource.IResourceService) {

    }

    getRebateResource(): ng.resource.IResourceClass<IRebateResource> {
        return this.$resource("http://localhost:48679/api/rebate/:id", null,
            {
                copy: { method: 'POST', url: "http://localhost:48679/copyrebate/:id"}
            }
        );
    }

}
angular.module("common.services")
    .service("dataAccessService",
    DataAcessService);
}

1 个答案:

答案 0 :(得分:0)

看起来我明白了。关键是从getCopyRebateResource方法返回时转换为接口类型

module rebateMaintenance.common {

interface IDataAccessService {
    getRebateResource(): ng.resource.IResourceClass<IRebateResource>;
    getCopyRebateResource(): ICopyRebateResourceClass;
}

interface IRebateResource
    extends ng.resource.IResource<rebateMaintenance.domain.IRebate> {
}

interface ICopyRebateResource
    extends ng.resource.IResource<domain.ICopyRebate> {
}

interface ICopyRebateResourceClass
    extends ng.resource.IResourceClass<any> {
    copy(params: Object, success?: Function, error?: Function): domain.ICopyRebate;
}

export class DataAcessService
    implements IDataAccessService {

    static $inject = ["$resource"];
    constructor(private $resource: ng.resource.IResourceService ) {

    }

    getRebateResource(): ng.resource.IResourceClass<IRebateResource> {
        return this.$resource("http://localhost:48679/api/rebate/:id", null,
            {
                'update': { method: 'PUT' }
            }
        );
    }

    getCopyRebateResource() {
        return     <ICopyRebateResourceClass>this.$resource("http://localhost:48679/copyrebate/:id", null,
            {
                copy: { method: 'POST' }
            }
        )
    }
}
angular.module("common.services")
    .service("dataAccessService",
    DataAcessService);
}