Typescript:Inject generic&获取ES6模块名称 [英] Typescript: Inject generic & get ES6 module name
问题描述
- Typescript
- ES6 li>
- Angular 1.x
但是我无法弄清楚我应该注入Entity和然后得到它的模块名称。
我想要获取名称的原因:
是因为我按照命名约定一个名为order-count.ts的文件应该呈现URL / / order / count'
这是可以用Typescript / Javascript解决的吗?
这是我有的:
order-module.ts
import {App} from'../../App';
import {OrderService} from'./order-service';
const模块:ng.IModule = App.module('app.order',[]);
module.service('orderService',OrderService);
order-service.ts
$从'../../shared/services/crud-service'
import {OrderCount}从'../order/'中导入{CrudService} $ {pre>
实体/顺序计数';
export class OrderService {
// @ngInject
constructor(private crudService:CrudService< OrderCount>){
this.crudService = crudService;
}
getOrders(){
var promise = this.crudService.getAll();
promise.then(response => {
console.log(response,'success');
},error => {
console.log (错误,'失败');
});
}
}
order-count.ts
import {Entity} from'../../../shared/models/entity';
export class OrderCount extends Entity {
storeId:string;
storeName:string;
}
entity.ts
export interface IEntity {
id:number;
}
entity.ts
import {IEntity} from'../../module/contracts/entities/entity';
export class Entity实现IEntity {
new(){}
id:number;
}
crud-service.ts
'use strict';
import {Entity} from'../models/entity';
import {EndpointService} from'./endpointService';
export class CrudService< TEntity extends Entity> {
private baseCallPath:string;
私有实体:{new():Entity};
// @ngInject
构造函数(private endpointService:EndpointService,private $ http:ng.IHttpService){
this.baseCallPath = new this.entity()。constructor.name .replace(' - ','/');
}
getAll():ng.IHttpPromise< any> {
return this.handleResponse(
this。$ http.get(this.endpointService.getUrl(this.baseCallPath)),
'getAll'
);
}
handleResponse(promise:ng.IHttpPromise< any>,callerMethodName:string):ng.IHttpPromise< any> {
return promise.success((data:any)=> {
Array.prototype.push.apply(this.baseCallPath,data);
})。error((reason: any)=> {
console.log(this.baseCallPath + callerMethodName,'ERROR',reason);
});
}
}
endpoint-service.ts
export class EndpointService {
private baseUri:string ='http:// localhost:3000 / api /' ;
getUrl(moduleName:string):string {
return this.baseUri + moduleName;
}
}
a href =https://codereview.stackexchange.com/questions/83107/automating-crud-using-repository-pattern-web-api-and-typescript>此链接可能有助于实施一个具有Typescript的通用存储库
I am trying to build a generic repository using:
- Typescript
- ES6
- Angular 1.x
But I can't figure out how I should inject the Entity and then get its module name.
The reason why i want to get the name: Is because i follow a naming convention where a file called order-count.ts should render the URL '/order/count'
Is this solvable with Typescript/Javascript?
Here is what i have:
order-module.ts
import {App} from '../../App';
import {OrderService} from './order-service';
const module: ng.IModule = App.module('app.order', []);
module.service('orderService', OrderService);
order-service.ts
import {CrudService} from '../../shared/services/crud-service'
import {OrderCount} from '../order/entities/order-count';
export class OrderService {
// @ngInject
constructor(private crudService: CrudService<OrderCount>) {
this.crudService = crudService;
}
getOrders() {
var promise = this.crudService.getAll();
promise.then(response => {
console.log(response, 'success');
}, error => {
console.log(error, 'failed');
});
}
}
order-count.ts
import {Entity} from '../../../shared/models/entity';
export class OrderCount extends Entity {
storeId: string;
storeName: string;
}
entity.ts
export interface IEntity {
id: number;
}
entity.ts
import {IEntity} from '../../module/contracts/entities/entity';
export class Entity implements IEntity {
new() { }
id: number;
}
crud-service.ts
'use strict';
import { Entity } from '../models/entity';
import { EndpointService } from './endpointService';
export class CrudService<TEntity extends Entity> {
private baseCallPath: string;
private entity: { new (): Entity };
// @ngInject
constructor(private endpointService: EndpointService, private $http: ng.IHttpService) {
this.baseCallPath = new this.entity().constructor.name.replace('-', '/');
}
getAll(): ng.IHttpPromise<any> {
return this.handleResponse(
this.$http.get(this.endpointService.getUrl(this.baseCallPath)),
'getAll'
);
}
handleResponse(promise: ng.IHttpPromise<any>, callerMethodName: string): ng.IHttpPromise<any> {
return promise.success((data: any) => {
Array.prototype.push.apply(this.baseCallPath, data);
}).error((reason: any) => {
console.log(this.baseCallPath + callerMethodName, 'ERROR', reason);
});
}
}
endpoint-service.ts
export class EndpointService {
private baseUri: string = 'http://localhost:3000/api/';
getUrl(moduleName: string): string {
return this.baseUri + moduleName;
}
}
This link may be helpful in order to implement a generic repository with Typescript
这篇关于Typescript:Inject generic&获取ES6模块名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!