Emberjs,rails - 活动模型适配器
Emberjs, rails - Active Model Adapter
我有一个 rails api 和 "active_model_serializers" gem。
http://localhost:3000/api/buildings/30.json 为我生成了这个:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
}
我还使用 EmberJs 和 ActiveModelAdapter。但是 Ember 期望这样:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
40,
69,
39
]
},
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
如何使 ember 使用 rails json 结构?谢谢!
您需要在 Ember 端使用 Embedded Records Mixin。
App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
foos: {embedded: 'always'}
}
});
查看 this answer
另一种选择是走 JSON API 路线。
我认为这是一个更好的选择,因为
- 您开始为 Ember 数据
的 current/future 格式编码
- 更清楚地映射和理解数据发生了什么
我在 http://emberigniter.com/modern-bridge-ember-and-rails-5-with-json-api/ 上写了一个指南(适用于 Rails 5),但您肯定可以根据您的 Rails
版本调整它
我有一个 rails api 和 "active_model_serializers" gem。
http://localhost:3000/api/buildings/30.json 为我生成了这个:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
}
我还使用 EmberJs 和 ActiveModelAdapter。但是 Ember 期望这样:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
40,
69,
39
]
},
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
如何使 ember 使用 rails json 结构?谢谢!
您需要在 Ember 端使用 Embedded Records Mixin。
App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
foos: {embedded: 'always'}
}
});
查看 this answer
另一种选择是走 JSON API 路线。
我认为这是一个更好的选择,因为
- 您开始为 Ember 数据 的 current/future 格式编码
- 更清楚地映射和理解数据发生了什么
我在 http://emberigniter.com/modern-bridge-ember-and-rails-5-with-json-api/ 上写了一个指南(适用于 Rails 5),但您肯定可以根据您的 Rails
版本调整它