Skip to content

Instantly share code, notes, and snippets.

@Mattchewone
Last active December 18, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattchewone/498d06e2679baaf4cc9c698a32fc2697 to your computer and use it in GitHub Desktop.
Save Mattchewone/498d06e2679baaf4cc9c698a32fc2697 to your computer and use it in GitHub Desktop.
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