如何自定义特定模型的Ember存储路径?

时间:2022-09-09 21:05:53

I have an API endpoint at /movies/:movie_id/actors.

我在/ movies /:movie_id / actors上有一个API端点。

I'm trying to use Ember Data to fetch this endpoint. I'm not interested in modelling movies at this point, just actors. My route looks like this:

我正在尝试使用Ember Data来获取此端点。我对此时的电影建模不感兴趣,只是演员。我的路线看起来像这样:

this.route('actors', { path: '/movies/:movie_id/actors' });

My actor model is plain:

我的演员模特很简单:

DS.Model.extend({
  name: DS.attr("name")
})

In my actors route, I have:

在我的演员路线中,我有:

model: function(params) {
  // params contains movie_id
  return this.store.findAll('actor')
}

This will cause Ember to send a request for /actors. How can I tell Ember to send a request to /movies/:movie_id/actors instead?

这将导致Ember发送/ actors的请求。如何告诉Ember向/ movies /:movie_id / actors发送请求?

My JSON is being returned in the format { "movies": [ { … } ] } and I'm using the DS.ActiveModelAdapter, if that's at all relevant. I'm using Ember 2.0.

我的JSON以{“movies”:[{...}]}格式返回,我正在使用DS.ActiveModelAdapter,如果这完全相关的话。我正在使用Ember 2.0。

1 个解决方案

#1


1  

DS.Store doesn't work around "path" concept. It's more of a data bucket, which - when supplemented - can take burden of working with provider (fetch/update/create/cache etc.) off developer. In your case it looks similar to this:

DS.Store不适用于“路径”概念。它更像是一个数据桶,当补充时,它可以承担与开发人员一起使用提供者(获取/更新/创建/缓存等)的负担。在你的情况下,它看起来类似于:

如何自定义特定模型的Ember存储路径?

ActiveModelAdapter, which you're using right now is using specific convention for accessing and isn't compatible with your data provider. So, what options do you have?

您正在使用的ActiveModelAdapter正在使用特定的约定来访问,并且与您的数据提供程序不兼容。那么,你有什么选择?

  1. Customize ActiveModelAdapter by overriding pathForType or buildURL methods (note - links are for RESTAdapter, since ActiveModelAdapter subclasses it)
  2. 通过重写pathForType或buildURL方法来自定义ActiveModelAdapter(注意 - 链接用于RESTAdapter,因为ActiveModelAdapter是它的子类)
  3. Choose more compatible adapter or even write your own
  4. 选择更兼容的适配器甚至自己编写
  5. Don't use adapter - fetch the data through AJAX and feed it to store directly using push()/pushPayload()
  6. 不要使用适配器 - 通过AJAX获取数据并使用push()/ pushPayload()直接将其提供给存储

#1


1  

DS.Store doesn't work around "path" concept. It's more of a data bucket, which - when supplemented - can take burden of working with provider (fetch/update/create/cache etc.) off developer. In your case it looks similar to this:

DS.Store不适用于“路径”概念。它更像是一个数据桶,当补充时,它可以承担与开发人员一起使用提供者(获取/更新/创建/缓存等)的负担。在你的情况下,它看起来类似于:

如何自定义特定模型的Ember存储路径?

ActiveModelAdapter, which you're using right now is using specific convention for accessing and isn't compatible with your data provider. So, what options do you have?

您正在使用的ActiveModelAdapter正在使用特定的约定来访问,并且与您的数据提供程序不兼容。那么,你有什么选择?

  1. Customize ActiveModelAdapter by overriding pathForType or buildURL methods (note - links are for RESTAdapter, since ActiveModelAdapter subclasses it)
  2. 通过重写pathForType或buildURL方法来自定义ActiveModelAdapter(注意 - 链接用于RESTAdapter,因为ActiveModelAdapter是它的子类)
  3. Choose more compatible adapter or even write your own
  4. 选择更兼容的适配器甚至自己编写
  5. Don't use adapter - fetch the data through AJAX and feed it to store directly using push()/pushPayload()
  6. 不要使用适配器 - 通过AJAX获取数据并使用push()/ pushPayload()直接将其提供给存储