Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Step 4.6: Add basic messages component
- Loading branch information
1 parent
4794e37
commit 4efc827
Showing
1 changed file
with
22 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
import {Component, OnInit} from "@angular/core"; | ||
import {NavParams} from "ionic-angular"; | ||
import {Chat} from "../../../../both/models/chat.model"; | ||
import {Messages} from "../../../../both/collections/messages.collection"; | ||
import {Observable} from "rxjs"; | ||
import {Message} from "../../../../both/models/message.model"; | ||
import template from "./messages-page.component.html"; | ||
|
||
@Component({ | ||
selector: "messages-page", | ||
template: `Messages Page` | ||
template | ||
}) | ||
export class MessagesPage implements OnInit { | ||
private selectedChat: Chat; | ||
private title: string; | ||
private picture: string; | ||
private messages: Observable<Message[]>; | ||
|
||
constructor(navParams: NavParams) { | ||
this.selectedChat = <Chat>navParams.get('chat'); | ||
|
||
console.log("Selected chat is: ", this.selectedChat); | ||
this.title = this.selectedChat.title; | ||
this.picture = this.selectedChat.picture; | ||
} | ||
|
||
ngOnInit() { | ||
let isEven = false; | ||
|
||
this.messages = Messages.find( | ||
{chatId: this.selectedChat._id}, | ||
{sort: {createdAt: 1}} | ||
).map((messages: Message[]) => { | ||
messages.forEach((message: Message) => { | ||
message.ownership = isEven ? 'mine' : 'other'; | ||
isEven = !isEven; | ||
}); | ||
|
||
return messages; | ||
}); | ||
} | ||
} |