Ember-data 嵌入记录当前状态? [英] Ember-data embedded records current state?

查看:22
本文介绍了Ember-data 嵌入记录当前状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被 Ember-data 造成的大量问题所困扰,而且它缺乏对嵌入式记录的支持.

I am really stuck with tons of problems caused by Ember-data and it lacks of embedded records support.

我搜索了整个网络,大多数帖子都过时了,其他帖子也过时了 + 需要我使用 3rd 方库或连接 300 行特殊代码,但有很多缺点.

I have searched the entire web, most of the posts are outdated others are outdated + requires me to use 3rd party libraries or to wire up 300 lines of special code with lots of drawbacks.

我不知道如何将嵌入的记录与今天的 ember-data 一起使用?

I've no idea how to use embedded records with ember-data as it stands today?

现在有一个新文档http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

推荐答案

使用 ActiveModelSerializer 您可以包含 EmbeddedRecordsMixin,它允许您使用嵌入的记录.(在 Canary 版本 1.0 beta 9+ 中,您也可以使用 JsonSerializer/RESTSerializer)

Using the ActiveModelSerializer you can include the EmbeddedRecordsMixin which allows you to use embedded records. (In the canary versions, 1.0 beta 9+, you can use the JsonSerializer/RESTSerializer as well)

App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    foos: {embedded: 'always'}
  }
});

模型

App.Color = DS.Model.extend({
  color: DS.attr(),
  foos: DS.hasMany('foo')
});

App.Foo = DS.Model.extend({
  name: DS.attr()
});

JSON

{
 colors:[
  {
    id: 1,
    color: "red",
    foos:[
      {
        id:1,
        name:'something 1'
      },
      {
        id:2,
        name:'something 2'
      }
    ]
  },
  ...

http://emberjs.jsbin.com/qagalabaso/1/edit

对于RESTSerializerJsonSerializer,它遵循相同的模式

For the RESTSerializer and JsonSerializer it follows the same pattern

App.ColorSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    foos: {embedded: 'always'}
  }
});

http://emberjs.jsbin.com/lesiwebobi/1/edit

这篇关于Ember-data 嵌入记录当前状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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