如何使用Ember自反模型连接Rails Grape API

时间:2015-10-05 15:38:30

标签: json ember.js ember-data grape grape-entity

我在使用Ember自反模型连接Grape(使用Grape实体序列化)的Json有效负载时遇到问题。模型看起来像这样:

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse 'children'
}

所以你可以看到我试图在这里建模类别 - 子类别关系。来自端点的示例json响应是:

{
  "category": {
    "id": 1,
    "name": "Sport",
    "child_ids": [
      5,
      6,
      8,
      7
    ]
  },
  "children": [
    {
      "id": 5,
      "name": "Basketball",
      "parent_id": 1
    },
    {
      "id": 6,
      "name": "Football",
      "parent_id": 1
    },
    {
      "id": 8,
      "name": "Running",
      "parent_id": 1
    },
    {
      "id": 7,
      "name": "Volleyball",
      "parent_id": 1
    }
  ]
}

有警告信息:

WARNING: Encountered "children" in payload, but no model was found for model name "child" (resolved model name using mateby-front@serializer:-active-model:.modelNameFromPayloadKey("children"))

正如在ActiveModelAdapter的文档中所说,它应该为嵌套资源提供id,并为带有sideloaded数据的父资源提供id。问题是在余烬检查员中有一个类别记录,但没有相关的孩子。

我还尝试制作两个模型:category和subcategory,并提供与其他id / id namings相同的有效负载,我可以看到在这种情况下,所有需要的Subcategory记录,但不是hasMany for Category(两个单独的数据) )。

1 个答案:

答案 0 :(得分:0)

问题可能来自于您忘记在第二个反面前放置双点的事实:

更改

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse 'children' #here
}

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse: 'children' #here
}