ember.js - Ember : Access sideloaded model relationship data in template

标签 ember.js view model ember-data relationship

我正在尝试从侧载数据的模板中显示模型的关系,但似乎存在一些问题。使用 Ember Inspector,我看到关系是从数据中正确加载的。但是,数据不会显示在页面上。

寻找有关从何处开始调试的解决方案或建议。非常感激。

Handlebars :

<dt>Categories</dt>
<dd>
  <ul>
    {{#each model.categories as |category| }}
      <li>{{ category.name }}</li>
    {{/each}}
  </ul>
</dd>

路线:
export default Ember.Route.extend(AuthenticatedRouteMixin, {
  model(params) {
    return this.store.findRecord('datasheet', params.id);
  }
});

型号:
// app/models/datasheet.js
export default DS.Model.extend({
  name: DS.attr('string'),
  name_en: DS.attr('string'),
  news: DS.attr('string'),
  news_en: DS.attr('string'),
  basic_information: DS.attr('string'),
  basic_information_en: DS.attr('string'),
  id_gradient_default: DS.attr('string'),
  icon_name: DS.attr('string'),
  icon_color: DS.attr('string'),
  order: DS.attr('string'),
  item_count: DS.attr('string'),

  categories: DS.hasMany('category')
});

// app/models/category.js
export default DS.Model.extend({
  name: DS.attr('string')
});

这是从适配器方法返回的 JSON:
{
    "data": {
        "type": "datasheet",
        "id": "21",
        "attributes": {
            "name": "Projekty",
            "name_en": "Projects",
            "news": "",
            "news_en": "",
            "basic_information": "",
            "basic_information_en": "",
            "id_gradient_default": "27",
            "icon_name": "pin_flag",
            "icon_color": "",
            "order": "14"
        },
        "relationships": {
            "categories": ["18", "19", "20", "51", "52"]
        }
    },
    "included": [{
        "type": "category",
        "id": "18",
        "attributes": {
            "name": "Project"
        }
    }, {
        "type": "category",
        "id": "19",
        "attributes": {
            "name": "Activity"
        }
    }, {
        "type": "category",
        "id": "20",
        "attributes": {
            "name": "Project phase"
        }
    }, {
        "type": "category",
        "id": "51",
        "attributes": {
            "name": "Program"
        }
    }, {
        "type": "category",
        "id": "52",
        "attributes": {
            "name": "Milestone"
        }
    }]
}

Ember 检查员截图:
enter image description here

最佳答案

这不是正确的 JSONAPI:

 "relationships": {
   "categories": ["18", "19", "20", "51", "52"]
 }

这是正确的 JSONAPI 等价物:
"relationships": {
  "categories": {
    data: [{
      id: '18',
      type: 'category'
    },{
      id: '19',
      type: 'category'
    },{
      id: '20',
      type: 'category'
    },{
      id: '51',
      type: 'category'
    },{
      id: '52',
      type: 'category'
    }]
  }
}

因此,您的数据已加载但未正确链接。当您检查 categories 时,您可以在 ember-inspector 中看到这一点关系。

关于ember.js - Ember : Access sideloaded model relationship data in template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34997150/

相关文章:

php - 使用 PDO 创建模型层

javascript - 我想使用句柄调用对象的特定实例,例如。{{ object.someAttribute 2 }}

swift - 如何将对象限制为全屏

SQL Server : setting language while creating view

apache-flex - Flex移动手势事件: how to slide between 2 views like Google+ Mobile?

swift - 如何在 iOS 中模拟不同的外形尺寸?

ember.js - 如何在 Ember.js 中刷新更改的值

Ember.JS ActiveModelAdapter 和 Active Model Serializers 默认行为

ember.js - 如何在 Ember 中测试路由的 willTransition Action ?

c++ - QItemSelectionModel::selectedIndexes() 崩溃