-
-
Save Mattchewone/498d06e2679baaf4cc9c698a32fc2697 to your computer and use it in GitHub Desktop.
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
Component.extend({ | |
ViewModel: { | |
msg: 'string', | |
to: 'string', | |
messages: 'any', | |
users: 'any', | |
get messagesAndUsers () { | |
return Promise.all([ | |
this.messagesPromise, | |
this.usersPromise | |
]) | |
}, | |
user: { | |
get: () => Session.current.user | |
}, | |
messagesPromise: { | |
default: () => Messages.getList({ $or: [{ from: Session.current.user._id }, { to: Session.current.user._id }] }) | |
}, | |
usersPromise: { | |
default: () => User.getList() | |
}, | |
sendMessage (event) { | |
event.preventDefault() | |
const toUser = this.users.usersByEmail[this.to] | |
if (toUser) { | |
new Messages({ name: this.msg, to: toUser._id }) | |
.save() | |
.then(() => { | |
this.msg = '' | |
this.to = '' | |
}) | |
} else { | |
console.log('No user found') | |
} | |
}, | |
updateMessage (event, message) { | |
event.preventDefault() | |
message.save() | |
}, | |
connectedCallback () { | |
// Listen to the session prop so we can load messages | |
// once we have an user | |
this.listenTo('user', (e, newVal) => { | |
// Load both users and messages and assign to VM | |
this.messagesAndUsers.then(([messages, users]) => { | |
this.messages = messages | |
this.users = users | |
}) | |
}) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment