So I am attempting to build a one on one chat app with firebase using marionette and backbone. I'm also using the Firebase Backbone bindings. I want there to be many users, each user has many conversation, and each conversation has many messages. Here's how my data looks:
users: {
'user-id-1': {
email: 'test@whatever.com',
conversations: {
'conversation-id-1': true,
'conversation-id-2': true
}
},
'user-id-2': {
email: 'another@email.com',
conversations: {
'conversation-id-1': true,
}
},
'user-id-3': {
email: 'a-third@email.com',
conversations: {
'conversation-id-2': true,
}
}
}
conversations: {
'conversation-id-1': {
sender: 'user-id-1',
receiver: 'user-id-2',
messages: {
'message-id-1': true,
'message-id-2': true
}
},
'conversation-id-2': {
sender: 'user-id-1',
receiver: 'user-id-3',
messages: {
'message-id-3': true,
'message-id-4': true,
'message-id-5': true,
}
}
}
messages: {
'message-id-1': {
sender: 'user-id-1'
body: 'A message'
},
'message-id-2': {
sender: 'user-id-2'
body: 'A response'
},
'message-id-3': {
sender: 'user-id-1'
body: 'An inital message'
},
'message-id-4': {
sender: 'user-id-3'
body: 'Another response'
},
'message-id-5': {
sender: 'user-id-1'
body: 'Goodbye'
},
}
What you can see from this:
User-1
has two different conversations going, each with User-2
and User-3
On a really basic level I am able to fetch all the conversations from the user with a function like so:
class Entities.User extends Backbone.Firebase.Model
urlRoot: 'https://my-app.firebaseio.com/users'
conversations: ->
conversations = new Firebase('https://my-app.firebaseio.com/conversations')
@firebase.child('conversations').on 'child_added', (snap) =>
console.log snap.key()
conversations(snap.key()).once 'value', (value) =>
console.log value.val()
undefined
This will log all of the conversations out when they are retrieved by firebase.
The problems are:
a) There's no real way for me to use a Backbone.Collection here to fetch just this users conversations, let alone the messages for that conversation, is there? I need a collection to pass to Marionettes CollectionView b) This just seems really messy in general.
Should I be structuring my data differently? I realize there's improvements that could be made to at least the way I am collecting the conversations so I could get an array.
What am I doing wrong? Any advice would be much appreciated.