无法从模型访问方法

时间:2017-10-24 15:22:34

标签: angular typescript

我有以下型号:

import { EventConsumerModel } from './EventConsumerModel';

export class EventLogModel {

  private id: number;
  private event_type: string;
  private occurred_on: string;
  private producedDomain: string;
  private eventConsumers: EventConsumerModel[];

  constructor(values: Object = {}) {
    Object.assign(this, values);
  }

  public getEventConsumers(): EventConsumerModel[] {
    return this.eventConsumers;
  }
}

export class EventConsumerModel {

  private status: string;

  constructor(values: Object = {}) {
    Object.assign(this, values);
  }

  public getStatus(): string {
    return this.status;
  }
}

我正在尝试从getStatus()内部访问EventConsumerModel方法。不幸的是,这不起作用。

如果我这样做:

for (let ec of this.events[0].getEventConsumers()) {
  console.log(ec.getStatus());
}

其中this.events的类型为EventLogModel[];我收到错误ec.getStatus is not a function

我填充this.events的服务如下:

public getEvents(batchId: string): Observable<EventLogModel[]> {
    return this.http.get(EVENTS_URL + batchId + EVENTS_ENDPOINT)
      .map((res: Response) => {
        const events = res.json().eventList;
        return events.map((e) => new EventLogModel(e));
      })
      .catch(this.handleErrorObservable);
}

,典型的JSON响应如下所示:

  "batchId": "2c9180855f359542015f35aa2d460d74",
  "status": "REJECTED",
  "createdTime": "2017-10-19T17:24:26Z",
  "eventList": [
    {
      "unique_id": 200168014,
      "id": "12091792-9020-4311-8bad-44fa8b69d218",
      "event_type": "com.adp.pi.eventmachine.PayFileProcessingCompleted",
      "partition_key": "2c9180855f359542015f35aa2d460d74",
      "occurred_on": "2017-10-19T17:24:26",
      "trace_id": "-8551048985939099152",
      "event_version": "1",
      "producedDomain": "PAY_FILE_STATUS",
      "eventConsumers": [
        {
          "status": "COMPLETED",
          "listener": "com.clientmanagement.hr.presentation.event.PayFileProcessingCompletedEventHandler",
          "consumedOn": "2017-10-19T17:24:27",
          "consumedDomain": "POLICY"
        }
      ],
      "consumedOn": "2017-10-19T17:24:27"
    }
  ]
}

任何想法我做错了什么?我最终希望能够使用HTML中的getStatus()函数进行绑定。

修改:看起来它永远不会创建EventConsumerModel。我在该模型的构造函数中放了一个console.log(),没有任何结果。可能与我在服务中映射对象的方式有关;但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

问题在于构造函数中的Object.assign(this, values);,它将输入映射到成员变量,但不会为您创建任何实例。因此,eventConsumers成员只是一个对象数组,而不是EventConsumerModel的实例数组。

因此,您必须明确设置eventConsumers的成员变量:

interface EventLogResponse {
  id?: number, 
  event_type?: string, 
  occured_on?: string, 
  producedDomain?: string,
  eventConsumers?: Object[]
}

export class EventLogModel {

  private id: number;
  private event_type: string;
  private occurred_on: string;
  private producedDomain: string;
  private eventConsumers: EventConsumerModel[];

  constructor({ 
    eventConsumers,
    ...obj
  }: EventLogResponse = {}) {
    Object.assign(this, obj);
    this.eventConsumers = eventConsumers.map(ec => new EventConsumerModel(ev));
  }

  public getEventConsumers(): EventConsumerModel[] {
    return this.eventConsumers;
  }
}