问题
Working on an app that has events
and each event
has many images
Here is the association:
images: DS.hasMany("event-image", { async: true })
The EventImage
Model:
`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`
When visiting /events/1722/edit
, I am [trying to] display the images like so:
<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>
The JSON API response, returns an attribute that looks like:
"image_ids": [
2474,
2469,
2468
]
When the page loads, I can see the three images on the page, but when attempting to fetch data to load each image, the API route that is called is /api/event_images/2469
. I need to add on the event_id
query param (or even better, have event_images
nested underneath events so that the API call is /events/:event_id/event_images
).
I am on EmberJS v1.10
and am using ActiveModelAdapter
. That looks like this:
`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`
I'm an EmberJS newb, dropped into this project, so my apologies here, trying to play catch up.
回答1:
if u want a different request url then conventional.then U can write a separate adapter for that modal. you have override
buildUrl method in adapter
From EmberJS Docs
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
来源:https://stackoverflow.com/questions/39381102/emberjs-hasmany-association-fetch-not-working-properly