【问题标题】:EmberJS hasMany association fetch not working properlyEmberJS hasMany 关联获取无法正常工作
【发布时间】:2017-01-15 19:20:28
【问题描述】:

在一个有events 的应用上工作,每个event 有很多images

这是关联:

images: DS.hasMany("event-image", { async: true })

EventImage 模特:

`import DS from 'ember-data'`

EventImage = DS.Model.extend
  event: DS.belongsTo("event")

  ### NATURAL ATTRIBUTES ###
  imageUrl: DS.attr("string", { defaultValue: "" })
  fileName: DS.attr("string")
  fileSize: DS.attr("string")
  primary: DS.attr("boolean", { defaultValue: false })
  featured: DS.attr("boolean", { defaultValue: false })

`export default EventImage`

访问/events/1722/edit 时,我[试图] 显示如下图像:

  <div class="field">
    <label class="label label__text">More images</label>

    {{#core-image-multiple-uploader model=model processedFiles=model.images}}
      Select additional event photos (10 max).
    {{/core-image-multiple-uploader}}
  </div>

JSON API 响应返回一个如下所示的属性:

"image_ids": [
  2474,
  2469,
  2468
]

当页面加载时,我可以看到页面上的三个图像,但是当尝试获取数据以加载每个图像时,调用的 API 路由是 /api/event_images/2469。我需要添加 event_id 查询参数(或者更好的是,将 event_images 嵌套在事件下方,以便 API 调用为 /events/:event_id/event_images)。

我在EmberJS v1.10 上使用ActiveModelAdapter。看起来像这样:

`import Ember from 'ember'`
`import DS from 'ember-data'`


ApplicationAdapter = DS.ActiveModelAdapter.extend
  host: window.EmberENV.apiURL
  headers:
    "Accept" : "application/json, text/javascript; q=0.01"

`export default ApplicationAdapter`

我是一名 EmberJS 新手,参与了这个项目,所以我在这里道歉,试图赶上。

【问题讨论】:

    标签: ember.js coffeescript


    【解决方案1】:

    如果您想要一个不同的请求 url,那么您可以为该模式编写一个单独的适配器。你已经覆盖了

    适配器中的buildUrl方法

    来自 EmberJS 文档

    buildURL (modelName, id, snapshot, requestType, query) String
    Inherited from DS.BuildURLMixin addon/-private/adapters/build-url-mixin.js:33
    Builds a URL for a given type and optional ID.
    
    By default, it pluralizes the type's name (for example, 'post' becomes 'posts' and 'person' becomes 'people'). To override the pluralization see pathForType.
    
    If an ID is specified, it adds the ID to the path generated for the type, separated by a /.
    
    When called by RESTAdapter.findMany() the id and snapshot parameters will be arrays of ids and snapshots.
    
    Parameters:
    modelName String
    id (String|Array|Object)
    single id or array of ids or query
    snapshot (DS.Snapshot|Array)
    single snapshot or array of snapshots
    requestType String
    query Object
    object of query parameters to send for query requests.
    Returns:
    String
    url
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多