Skip to content

Instantly share code, notes, and snippets.

@Mattchewone Mattchewone/message-list--vm.js Secret
Last active Dec 18, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.