vendredi 24 avril 2015

How to update record locally with Ember-Data and Ember?

I'm building a chat based application which allows users to view message history, send new messages and select from recent users. The recent user list is being sorted by a recent_rating attribute which is the date of the last sent/received message related to that user. The recent_rating is not an attribute in the database, but a method being declared within the user model. The users are being ordered properly when I load the page for the first time with the following set up in my code:

Serializer:

class UserSerializer < ActiveModel::Serializer
    attributes :id, :email, :name, :recent_rating
end

Rails model:

    def recent_rating
        Message.where('recipient_id = :user_id OR sender_id = :user_id', user_id: id).last.try(:created_at)
    end

Ember Controller:

App.ChatsController = Ember.ArrayController.extend(
{
    sortProperties: ['recent_rating'],
    sortAscending: false,

    ....
});

Ember model:

App.User = DS.Model.extend(
{
  name: DS.attr('string'),
  email: DS.attr('string'),
  recent_rating: DS.attr('string'),
});

However, when I send a new message, I need to update the recent_rating attribute in the local ember-data storage for the related user record. This would in turn update the list ordering for recent users.

How can I update the attribute for a record in Ember-Data and avoid the API POST request? As recent_rating doesn't exist as an attribute in the database on the API side

Thanks




Aucun commentaire:

Enregistrer un commentaire