【问题标题】:Ember Data 2.1.0 ignores the links property in my JSON responseEmber Data 2.1.0 忽略了我的 JSON 响应中的 links 属性
【发布时间】:2015-10-09 23:49:13
【问题描述】:

Ember Data 2.1.0 会忽略我的 JSON 响应中的 links 属性,而是触发以下请求:

/point_logs/3e5ff053422f40e3a8057fc5e8100c47

对于每一个发现的点日志,它都会触发一个请求。相反,我希望它获取所有点日志的集合。

尽管在 Ember 1.x 中一切正常。到处寻找答案,但不幸的是,到目前为止我无法找到解决方案。

设备模型

var DirectObject = DS.Model.extend({
    name: DS.attr('string'),
    description: DS.attr('string'),
    type: DS.attr('string'),
    createdDate: DS.attr('date'),
    modifiedDate: DS.attr('date'),
    deletedDate: DS.attr('date'),
    pointLog: DS.belongsTo('pointLog')
});

点对数模型

var PointLog = DS.Model.extend({
    unit: DS.attr('string'),
    type: DS.attr('string'),
    lastConsecutiveLogDate: DS.attr('date'),
    updatedDate: DS.attr('date'),
    directObject: DS.belongsTo('directObject')
});

JSON 响应

{
  "data": [
    {
      "id": "6dbcf32a3e064a36a1db4847329cc90d",
      "type": "appliance",
      "attributes": {
        "name": "3974737",
        "description": "",
        "type": "zz_misc",
        "createdDate": "2015-09-15T14:23:02.768Z",
        "modifiedDate": "2015-10-08T08:39:15.525Z",
        "deletedDate": null
      },
      "relationships": {
        "pointLog": {
          "data": {
            "id": "3e5ff053422f40e3a8057fc5e8100c47",
            "type": "pointLog"
          },
          "links": {
            "related": "/core/appliances/6dbcf32a3e064a36a1db4847329cc90d/point_log"
          }
        }
      }
    }
  ],
  "included": [],
  "links": {
    "self": "/core/appliances"
  }
}

我也尝试过使用 self 属性而不是相关属性。并且还作为一个对象相关,其中一个 href 属性是 url。

【问题讨论】:

  • 设备扩展了 DirectObject 模型
  • 如何为您的数据提供服务?是异步的吗?澄清一下:您想为每个设备返回多个pointLogs?反之亦然?
  • @sbatson5 调用是异步的,实际上我想为每个设备返回多个 pointLogs。

标签: javascript ember.js ember-data


【解决方案1】:

好的,我想通了。问题是数据属性。似乎如果它存在,Ember 将在不使用链接对象中给出的 url 的情况下进行单独调用。

之前

"relationships": {
  "pointLog": {
    "data": {
      "id": "3e5ff053422f40e3a8057fc5e8100c47",
      "type": "pointLog"
      },
      "links": {
        "related": "/core/appliances/6dbcf32a3e064a36a1db4847329cc90d/point_log"
    }
  } 
}

之后

"relationships": {
  "pointLog": {
    "links": {
      "related": "/core/appliances/6dbcf32a3e064a36a1db4847329cc90d/point_log"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多