尝试在ember.js中自引用模型时出错

时间:2017-03-31 20:43:28

标签: javascript c# json asp.net-web-api ember.js

我正在尝试在模型中创建自引用属性,但是我收到以下错误:

  

处理路线时出错:index未找到'menu'错误的模型

这是我的模型(menus.js):

import DS from 'ember-data';

export default DS.Model.extend({    
  title: DS.attr('string'),
  link: DS.attr('string'),
  isActive: DS.attr('boolean'),
  children: DS.hasMany('menus', { inverse: null }),
});

我正在尝试从Web API返回列表。这是我的API JSON返回:

[
  {
    "id": 1,
    "title": "Test",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 4,
        "title": "Test 2",
        "link": "index",
        "isActive": false
      }
    ]
  },
  {
    "id": 2,
    "title": "Test 2",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 5,
        "title": "Test 4 ",
        "link": "index",
        "isActive": false
      }
    ]
  },
  {
    "id": 3,
    "title": "Test 5",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 6,
        "title": "Test 6",
        "link": "index",
        "isActive": false
      },
      {
        "id": 7,
        "title": "Test 7",
        "link": "index",
        "isActive": false
      }
    ]
  }
]

1 个答案:

答案 0 :(得分:2)

你应该以单数形式打电话给你的模特。因此,请将model/menus.js重命名为model/menu.js,将DS.hasMany('menus', { inverse: null }),重命名为DS.hasMany('menu', { inverse: null }),