【问题标题】:FindAll method listing child content instead of only root elementsFindAll 方法列出子内容而不仅仅是根元素
【发布时间】:2017-04-05 04:24:23
【问题描述】:

我有一个 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
      }
    ]
  }
]

当我尝试在 Ember 中调用“findAll”方法时,它会列出根目录中的子菜单:

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

我只想带上根元素:“Test”、“Test 3”和“Test 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 })
});

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 2013-01-14
      • 1970-01-01
      • 2021-07-21
      • 2014-01-14
      相关资源
      最近更新 更多