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

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

问题描述



我已经搜索整个网页,大部分的帖子是过时的其他人已经过时了+需要我使用第三方库或连线300行特殊代码有很多缺点。



我不知道如何使用嵌入式记录与ember数据今天站在一起?



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

解决方案

使用 ActiveModelSerializer 可以包含 EmbeddedRecordsMixin 允许您使用嵌入式记录。 (在Canary版本1.0 Beta 9+中,您可以使用 JsonSerializer / RESTSerializer



Serializer



  App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin ,{
attrs:{
foos:{embed:'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



对于 RESTSerializer JsonSerializer ,它遵循相同的模式

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

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


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

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.

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

edit: there is a new documentation now http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

解决方案

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)

Serializer

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

Models

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

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数据嵌入记录当前状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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