vendredi 8 mai 2015

Loading part of an associated array in Ember CLI

I am building an Ember CLI chat app and am struggling with what should be a common problem. The setup is as follows:

  • I have two objects: conversations and messages. A conversation has many messages (via a DS.hasMany('message',{async:true}) relationship).
  • I have a conversations/show route. The route's model function loads a conversation object.
  • Messages are displayed in the template using something like:

    {{#each message in model.messages}} {{message.body}} {{/each}}

  • I have a websockets type setup that can push new messages into the model.messages object, and update the template.

This works well. However, if there are 100 messages in a conversation I don't want to load all of them as this is slow. Rather I want to load 10 at a time, with a 'load more messages' button in my template. To get around this I define a 'loadedModels' object in the route, load the last 10 messages, and loop over the 'loadedModels' object rather than 'model.messages' in the template.

The problem appears when a new object is pushed to the model.messages array via websockets. I want to define an observer like this:

function(){
    // Push new objects in model.messages into loadedModels
}.observes('model.messages.@each')

In this setup, how do I get the NEW messages in model.messages without loading the whole model.messages array? I cannot find a way to do this that does not load all associated messages.

Many thanks




Aucun commentaire:

Enregistrer un commentaire