具有ember数据的非CRUD操作 [英] Non CRUD actions with ember-data

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

问题描述

说我有以下ember数据模型:

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

这与Rails应用与以下漂亮的标准CRUD API进行通信:

  GET / people  - 获取人员列表
POST / people - 创建新人
GET / people / id - 获取特定人
PUT / people / id - 更新某个人
DELETE / people / id - 删除某个人

所有这些都使用标准的商店/适配器映射到Ember-Data。



但是,让我们说,为了明星或unstar一个人,API不让我们通过标准的更新操作来做到这一点。此操作有一个特定的API端点:

  POST / people / id / star  - 将某人标记为已加星标
POST / people / id / unstar - 将某人标记为未标记

我如何适应这个API与Ember数据?



看起来我需要扩展DS.Store和DS.RESTAdapter,但我不知道最好的方法让他们意识到这些不同的行为。这个应用程序的通用适配器必须知道主持人的感觉也有点错误。



请注意,我无法控制API,所以我可以'要使 POST / people / id 知道主演,以便它符合标准更新。

解决方案

在讨论组中,叶胡达提到这是不可能的。


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')
});

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

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

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"

How do I fit this API in with Ember Data?

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.

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.

解决方案

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

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

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