我将 Laravel 4 与 emberjs 一起使用,我需要在 emberjs 应用程序上列出关系数据的集合,但 L4 打印集合有点不同,因此,我正在尝试更改 REST 序列化,但目前没有任何工作.. 我的 Ember 数据版本是 1.0.0-beta.7+canary.238bb5ce。 有人帮忙吗?
我的 JSON 数据:
{
"names": [
{
"id": "1",
"description": "List 2014",
"user_id": "3",
"squares": [
{
"id": "1"
}
]
}
],
"squares": [
{
"id": "1",
"name": "squa1",
"role_id": "1"
},
{
"id": "2",
"name": "squa2",
"role_id": "1"
}
]}
我的 models.js:
App.NameSerializer = DS.RESTSerializer.extend({
primaryKey: 'id',
extractArray: function(store, type, payload, id, requestType) {
var posts =payload.names;
var squares = [];
payload.names[0].description = payload.names[0].description+"!!!";
the = payload;
posts.forEach(function(post){
var reporter = post.squares,
reporterId = reporter.id;
squares.push(reporter);
post.reporter = reporterId;
});
payload.squares = squares;
return this._super(store, type, payload, id, requestType);
}
});
App.Name = DS.Model.extend({
description: DS.attr('string'),
squares: DS.hasMany('square'),
});
App.Square = DS.Model.extend({
name: DS.attr('string'),
});
最佳答案
实际上,到目前为止您所做的 json 的唯一问题是方形 id
数组。
"squares": [ { "id": "1" } ]
Ember 数据正在等待
"squares": [ "1" ]
这可以通过您所采取的方式轻松解决
App.NameSerializer = DS.RESTSerializer.extend({
primaryKey: 'id', // note this isn't necessary, the primary key is id by default
extractArray: function(store, type, payload, id, requestType) {
payload.names.forEach(function(name){
var validSquareIdArray = [];
name.squares.forEach(function(square){
validSquareIdArray.push(square.id);
});
name.squares = validSquareIdArray;
});
return this._super(store, type, payload, id, requestType);
}
});
关于javascript - 恩博数据1.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366924/