[英]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. 最近用户列表按“
recent_rating
属性排序,该属性是与该用户相关的最后发送/接收的消息的日期。 The recent_rating
is not an attribute in the database, but a method being declared within the user model. recent_rating
不是数据库中的属性,而是在用户模型中声明的方法。 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: Rails模型:
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. 但是,当我发送新消息时,我需要为相关用户记录更新本地余烬数据存储中的
recent_rating
属性。 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? 如何更新Ember-Data中记录的属性并避免API POST请求? As
recent_rating
doesn't exist as an attribute in the database on the API side 由于
recent_rating
在API端数据库中作为属性不存在
Thanks 谢谢
There is a technical way to do what you're asking ( store#push ), but I think the design is a bit off here. 有一种技术方法可以完成您要问的事情( store#push ),但是我认为这里的设计有些偏离。
Here's how I would model this. 这是我要对此建模的方式。 A
user
hasMany messages
, and a message
has a createdAt
property. user
有多条messages
,并且一条message
具有createdAt
属性。 Then ordering the users by latest message becomes a matter of a few simple computed properties: 然后,通过最新消息对用户进行排序就变成了一些简单的计算属性:
App.Message = DS.Model.extend({
user: DS.belongsTo('user'),
createdAt: DS.attr('date')
});
App.User = DS.Model.extend({
messages: DS.hasMany('message'),
name: DS.attr('string'),
email: DS.attr('string'),
messagesSorting: ['createdAt:desc']
orderedMessages: Ember.computed.sort('messages', 'messagesSorting'),
latestMessage: Ember.computed.alias('orderedMessages.firstObject')
});
Now in your controller/component (whatever's displaying the users), you can create a new computed property to sort the users: 现在,在控制器/组件中(无论显示用户如何),您都可以创建一个新的计算属性来对用户进行排序:
App.SomeController = Ember.Controller.extend({
usersSorting: ['latestMessage:desc'],
sortedUsers: Ember.computed.sort('model', 'usersSorting')
});
and use that property in your template. 并在模板中使用该属性。
PS you should start using Ember CLI if possible :) PS,如果可能,您应该开始使用Ember CLI :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.