使用主干关系数据库处理JSON-API数据
我们一直在使用主干关系对前端的ORM关系进行建模,例如:使用主干关系数据库处理JSON-API数据,json,backbone.js,backbone-model,backbone-relational,Json,Backbone.js,Backbone Model,Backbone Relational,我们一直在使用主干关系对前端的ORM关系进行建模,例如: { id: 2 username: "bob" comments: [ { id:5, comment: "hi", user: { username: 'Bob' } } ] } 在前端使用这样的模型,效果非常好: class User extends App.RelationalModel rela
{
id: 2
username: "bob"
comments: [
{
id:5,
comment: "hi",
user: {
username: 'Bob'
}
}
]
}
在前端使用这样的模型,效果非常好:
class User extends App.RelationalModel
relations: [{
type: Backbone.HasMany,
key: 'comments',
relatedModel: 'Comment',
collectionType: 'CommentCollection'
}]
然而,现在我们的api已经改变,并且更多地遵守了JSON-api规范,因此来自后端的数据被封装在“数据”中
{
data: {
id: 2
username: "bob"
data: {
comments: [
{
id:5,
comment: "hi",
user: {
username: 'Bob'
}
}
]
},
meta: {
}
}
}
我们如何指示主干关系从.data获取“comments”关系的数据,而不是直接映射json结构
对于“类用户”,我们可以实现如下解析方法
class User
parse: (response) ->
response.data
但是我们如何处理评论关系呢?这是怎么回事
parse: function (response) {
var fixed_response = response.data;
fixed_response.comments = fixed_response.data.comments;
delete fixed_response.json.data;
return fixed_response;
}