带有 ember-data 的非 CRUD 操作 [英] Non CRUD actions with ember-data

查看:27
本文介绍了带有 ember-data 的非 CRUD 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下余烬数据模型:

Say I have the following ember-data model:

App.Person = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName:  DS.attr('string'),
    starred:   DS.attr('boolean')
});

这使用以下非常标准的 CRUD API 与 Rails 应用程序通信:

This communicates with a Rails app with the following pretty standard CRUD API:

GET    /people    - get a list of people
POST   /people    - create a new person
GET    /people/id - get a specific person
PUT    /people/id - update a specific person
DELETE /people/id - delete a specific person

这一切都通过标准的 Store/Adapter 映射到 Ember-Data.

This all maps to Ember-Data with the standard Store/Adapter.

但是,让我们说为了给某人加星标"或取消加星标",API 不允许我们通过标准更新操作来执行此操作.此操作有一个特定的 API 端点:

However, lets say that in order to "star" or "unstar" a person, the API doesn't let us do this by the standard update action. There's a specific API endpoint for this action:

POST   /people/id/star   - mark a person as "starred"
POST   /people/id/unstar - mark a person as "unstarred"

如何将此 API 与 Ember Data 配合使用?

How do I fit this API in with Ember Data?

看起来我需要以某种方式扩展 DS.Store 和 DS.RESTAdapter,但我不确定让他们知道这些不同操作的最佳方法.感觉也有点不对,app 的通用 Adapter 必须要知道主演的人.

It looks like I'd need to extend DS.Store and DS.RESTAdapter somehow, but I'm not sure of the best approach to make them aware of these different actions. It also feels a bit wrong that a generic Adapter for the app has to be aware of starring people.

请注意,我无法控制 API,因此我无法让 POST/people/id 知道starring",以便它适合标准更新.

Note that I have no control over the API, so I can't make POST /people/id aware of "starring" so that it would fit in with a standard update.

推荐答案

在讨论组中,Yehuda 提到这是不可能的.

In the discussion group, Yehuda mentioned this isn't possible yet.

这篇关于带有 ember-data 的非 CRUD 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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