Custom request urls in Ember model

2019-07-17 00:51发布

问题:

I am trying to use Ember data with already built REST api. It works fine for top level routes, for example I have route for courses on api side like following:

app.get('/courses', app.controllers.courses.findAll)
app.get('/courses/:id', app.controllers.courses.findById)

Above works fine and I can easily get data with Ember Data. But problem arises with routes that have multiple levels. For example, courses can have multiple topics and topics can have multiple lectures so routes for them are defined like this on api side.

Topics:

app.get('/courses/:course_id/topics', app.controllers.topics.findAll)
app.get('/courses/:course_id/topics/:id', app.controllers.topics.findById)

Lectures:

app.get('/courses/:course_id/topics/:topic_id/lectures', app.controllers.lectures.findAll)
app.get('/courses/:course_id/topics/:topic_id/lectures/:id', app.controllers.lectures.findById)

So if I want to get all lectures inside a course I need to specify course id and topic id as well (not in the query but in url body, as you can see from url structures in backend api).

In Ember I have models for course, topic and lecture but I dont know how can I specify custom urls so that Ember Data can use those urls when I make requests.

One way could be to manually make ajax requests but this way records will not be populated in Ember Data Store.

Or I could have defined relationships between models in Ember but that would require changes on backend api also which is not an option for me.

So is there any nice way to solve this problem?

I am using:

Ember: v1.6.0-beta.2

Ember-Data: v1.0.0-beta.7

回答1:

Avoid Ember Data, Ember works just fine without it.

If you really want to use it, make ajax calls then use store.pushPayload('type', data) to sideload the data into the store if you really want it in the store.