FindAll方法列出子内容而不是仅列出根元素

时间:2017-04-04 19:52:23

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

我有一个Web API返回上面的JSON的Ember应用程序:

[
  {
    "id": 1,
    "title": "Test",
    "link": "index",
    "isActive": false,
    "children": [
      {
        "id": 4,
        "title": "Test 2",
        "link": "index",
        "isActive": false
      }
    ]
  },
  {
    "id": 2,
    "title": "Test 3",
    "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
      }
    ]
  }
]

当我尝试拨打" findAll"在Ember中的方法,它列出了子菜单,因为它们在root中:

model() {
      return this.get('store').findAll('menu').then(function(menus){
        menus.forEach(function (item, index, enumerable) {
          console.log(item.get('title'));
        });
      });
  }

控制台结果:

Test 2
Test 4
Test 6
Test 7
Test
Test 3
Test 5

我想带上根元素:"测试","测试3"和"测试5"。其余的将是相应父级的子属性。这是我的" menu.js"

import DS from 'ember-data';

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

1 个答案:

答案 0 :(得分:0)

如果您不想要所有类型的记录,请不要使用findAll。使用query