序列化异步有很多关系 [英] Serialising async hasMany relationships

查看:139
本文介绍了序列化异步有很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于EmberJS来说,我很新,而且在大部分时间里,我一直都在忙着试图在一个新的应用程序中坚持一些许多和许多关系。



我正在使用Ember Data with ActiveModelAdapter 将其连接到我的rails后端,这是使用 ActiveModelSerializers



我正在使用以下版本的Ember和Ember数据,我正在使用。

  DEBUG:------------------------------- 
DEBUG :Ember:1.6.0-beta.1 + canary.d0f5f254
DEBUG:Ember数据:1.0.0-beta.7 + canary.d5562867
DEBUG:Handlebars:1.1.1
DEBUG :jQuery:1.10.2
DEBUG:-------------------------------

我一直存在的基本问题是,在保存记录时,关联ID不会被发送回服务器。 / p>

我潜入 ActiveModelSerializer源,并发现它跳过hasMany序列化

  var ActiveModelSerializer = RESTSerializer.extend({// ... 
/ **
默认情况下不会序列化hasMany关系。
* /
serializeHasMany:Ember.K,
}

我已经提出了以下基本解决方案,这是迄今为止的工作,但我想知道是否有一个更清洁的解决方案遵循Ember惯例和最佳实践。

  DS.ActiveModelAdapter.reopen 
命名空间:'api / v1'

App.Store = DS.Store.extend
适配器:' - 活动模型'

App.ApplicationSerializer = DS.ActiveModelSerializer.extend
serializeHasMany :((记录,json,关系) - >
如果relation.options.async
key = relationship.key
data = record.get(data。#{key})
如果数据?
json [@keyForRelationship(key,hasMany)] = data.mapBy(Ember.get(this,primaryKey))
return
pre>

解决方案

根据我的理解,活动模型序列化器跳过的原因有很多关系,它期望后端处理它。在关系数据库中,具有许多值的值未显式保存在父记录上。保存孩子的外键就足够了。当您要求更清洁的解决方案 - 或许 ActiveModelSerializer 不适合您的API。


I'm fairly new to EmberJS, and I've been fiddling around for most of the evening trying to persist some hasMany and many to many relationships in a new app that I'm working on.

I'm using Ember Data with ActiveModelAdapter to hook it up to my rails backend which is using ActiveModelSerializers

I'm using the following versions of Ember and Ember Data that I'm using.

DEBUG: -------------------------------
DEBUG: Ember      : 1.6.0-beta.1+canary.d0f5f254
DEBUG: Ember Data : 1.0.0-beta.7+canary.d5562867
DEBUG: Handlebars : 1.1.1
DEBUG: jQuery     : 1.10.2
DEBUG: ------------------------------- 

The basic problem I've been having is that when saving records, the association ids aren't being sent back to the server.

I took a dive into the ActiveModelSerializer source, and found that it skips hasMany serialization

var ActiveModelSerializer = RESTSerializer.extend({ // ...
  /**
    Does not serialize hasMany relationships by default.
  */
  serializeHasMany: Ember.K,
}

I've come up with the following rudimentary solution, which is working so far, but I was wondering if there was a cleaner solution that follows Ember convention and best practices.

DS.ActiveModelAdapter.reopen
  namespace: 'api/v1'

App.Store = DS.Store.extend
  adapter: '-active-model'

App.ApplicationSerializer = DS.ActiveModelSerializer.extend
  serializeHasMany: (record, json, relationship) ->
    if relationship.options.async
      key = relationship.key
      data = record.get("data.#{key}")
      if data?
        json[@keyForRelationship(key, "hasMany")] = data.mapBy(Ember.get(this, "primaryKey"))
    return

解决方案

In my understanding, the reason that the active model serializer skips has many relations is that it expects the backend to handle it. In a relational database, the values of a has many are not explicitly saved on the parent record. It is sufficient to save the foreign key in the child. When you ask for a cleaner solution - perhaps ActiveModelSerializer is not the best fit for your API.

这篇关于序列化异步有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆