I am trying to map the json returned from the backend to proper typescript classes on the angular side.
Here is my backend code:
findMessagesWithOtherUserAccount(otherId:Number):Observable<Message[]> {
return this.http.get('/api/message/find-messages-with-other-useraccount/' + otherId)
.map(this.extractMessages);
}
private extractMessages(res:Response) {
let body = res.json();
return body || {};
}
Notice the Observable
of Message
. Here Message
is the type I need because I have added business logic to the class as follows:
import {UserAccount} from "../useraccount/useraccount.model";
export class Message {
constructor(id:number,
sender:UserAccount,
recipient:UserAccount,
sendDate:Date,
messageRead:boolean,
text:string) {
this.id = id;
this.sender = sender;
this.recipient = recipient;
this.sendDate = sendDate;
this.messageRead = messageRead;
this.text = text;
}
id:number;
sender:UserAccount;
recipient:UserAccount;
sendDate:Date;
messageRead:boolean;
text:string;
getCounterparty(user:UserAccount):UserAccount {
if (!this.sender) return null;
return (user.id !== this.sender.id) ? this.sender : this.recipient;
}
isSender(user:UserAccount):boolean {
return user.id === this.sender.id;
}
isRecipient(user:UserAccount):boolean {
return user.id === this.recipient.id;
}
isNew(user:UserAccount):boolean {
return !this.messageRead && this.isRecipient(user);
}
}
I try to refer to the isRecipient
method from a component:
getSenderFirstName(message:Message):string {
if (message.isRecipient(this.currentUserAccount)) {
return this.otherUserAccount.firstName;
}
return 'Moi';//FIXME: i18n/translate
}
However, I get this error:
browser_adapter.js:81 TypeError: message.isRecipient is not a function
at MessageConversationComponent.getSenderFirstName (message-conversation.component.js:50)
Indicating that message is not typed (other than a plain js object)...
Here is the full component:
export class MessageConversationComponent implements OnInit {
messagesWithOtherUserAccount:Message[];
currentUserAccount:UserAccount;
otherUserAccount:UserAccount;
constructor(private messageService:MessageService,
private userAccountService:UserAccountService,
private routeSegment:RouteSegment) {
}
ngOnInit() {
this.messageService.findMessagesWithOtherUserAccount(2)
.subscribe(param=>this.messagesWithOtherUserAccount = param);
this.userAccountService.retrieveOtherUserAccount(2)
.subscribe(param=> this.otherUserAccount = param);
this.userAccountService.currentUserAccount$.subscribe(param=>this.currentUserAccount = param);
}
getSenderFirstName(message:Message):string {
if (message.isRecipient(this.currentUserAccount)) {
return this.otherUserAccount.firstName;
}
return 'Moi';//FIXME: i18n/translate
}
}
and the template:
<div *ngFor="let message of messagesWithOtherUserAccount" class="media col-xs-12">
<div class="media-body Lui" ng-class="getMessageClasses(message)">
<div class="media-heading">
{{getSenderFirstName(message)}} <small><span am-time-ago="message.sendDate"></span></small>
</div>
<p class="message-text">{{message.text}}</p>
</div>
</div>