在Ember模型中自定义请求网址 [英] Custom request urls in Ember model

查看:80
本文介绍了在Ember模型中自定义请求网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用已经构建的REST api的Ember数据。它适用于顶级路线,例如我在api侧有路线,如下所示:

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)

以上工作正常,我可以使用Ember Data轻松获取数据。但问题出现在具有多个级别的路由。例如,课程可以有多个主题和主题可以有多个讲座,因此他们的路线在api侧如此定义。

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.

主题:

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

讲座: / p>

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)

所以如果我想在课程中获得所有讲座,我还需要指定课程ID和主题ID(不是在查询中,而是在url正文中,你可以从后端api中的url结构)

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).

在Ember中,我有课程,主题和讲座的模型,但我不知道如何指定自定义网址,以便Ember Data可以使用url当我发出请求。

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.

一种方法可能是手工制作ajax请求,但是这种方式将不会在Ember Data Store中填充记录。

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

或者我可以在Ember中定义模型之间的关系,但这需要对后端api的更改,这对我来说不是一个选择。

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?

我正在使用:

Ember:v1.6.0-beta.2

Ember: v1.6.0-beta.2

Ember-Data:v1.0.0-beta.7

Ember-Data: v1.0.0-beta.7

推荐答案

避免使用Ember Data,Ember可以正常工作。

Avoid Ember Data, Ember works just fine without it.

如果您真的想使用它,请使用ajax调用,然后使用 store.pushPayload('type',data)如果您真的想在商店中将数据加载到商店。

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.

这篇关于在Ember模型中自定义请求网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆