mardi 23 août 2016

Side-loading associations in pushPayload method (Ember)

I have an Ember CLI chat app with a Conversation object that has many Message objects. What I want to do is load all existing messages in the conversation on page load, then add new messages using a WebSockets connection. The setup looks like this:

Step 1. Load relevant messages in the route, and side-load associations

model: function(params){
  return {
    messages: this.store.query('message', {
      conversation_id: params.conversation_id,
      include: 'user,conversation'
    }),
    conversation_id: params.conversation_id
  }
}

Step 2: Create a messages array using peekAll and a filter, which should update dynamically when new messages are added:

setupController: function(controller, model){
    this._super(controller, model);
    controller.set('messages', this.store.peekAll('message').filter(function(x){
      return x.get('conversation.id').toString() === model.conversation_id.toString();
    }));
})

Step 3: When a message is received add it to the array:

function received(data) {
  this.store.pushPayload(data);
}

Step 4: Display the array

// messages.hbs

  


This almost works, but I am having trouble side loading the associated conversation object when I do store.pushPayload(data). The object pushed to the store doesn't have a loaded Conversation object, so is not filtered correctly when using peekAll('message').filter(...). Is there a way to pass associations in to the pushPayload method?

Many thanks




Aucun commentaire:

Enregistrer un commentaire