我使用带有emberjs的Laravel 4,我需要在emberjs app上列出关系数据的集合,但是L4打印集合有点不同,所以,我试图改变REST序列化但没有工作目前.. 我的Ember Data版本是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'),
});
答案 0 :(得分:0)
对于你到目前为止所做的事情,json唯一的问题是方形id
的数组。
"squares": [ { "id": "1" } ]
Ember Data期待
"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);
}
});