具有成员数据的非CRUD操作

Non CRUD actions with ember-data

本文关键字:CRUD 操作 数据 成员      更新时间:2023-09-26

假设我有以下成员数据模型:

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

这与带有以下相当标准的CRUD API的Rails应用程序进行通信:

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

这一切都映射到Ember数据与标准存储/适配器。

然而,让我们说,为了"明星"或"取消明星"一个人,API不允许我们通过标准的更新操作来做到这一点。此操作有一个特定的API端点:

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

如何将此API与Ember数据相匹配?

看起来我需要以某种方式扩展DS.Store和DS.RESTAdapter,但我不确定让它们知道这些不同操作的最佳方法。应用程序的通用适配器必须意识到主角,这也让人觉得有点错误。

请注意,我不能控制API,所以我不能让POST /people/id知道"主演",这样它就可以适应标准更新。

已经有一段时间了,当时可能还不可能,但您可以直接调用适配器的ajax方法:

YourApp.Store.prototype.adapter.ajax(url, action, {data: hashOfParams})

例如:

YourApp.Store.prototype.adapter.ajax('/products', 'GET', {data: {ids: [1,2,3]}})

对于您的问题:

YourApp.Store.prototype.adapter.ajax('/people' + id + '/star','POST')

编辑-使用buildURL非常有用,特别是如果您在适配器上设置了名称空间:

url = YourApp.Store.prototype.adapter.buildURL('people',id) + '/star'

编辑2-您也可以使用container.lookup('adapter:application')获得适配器,如果您没有对应用程序(例如ES6模块/ember-cli)的全局访问权限,这将非常有用

Edit 3-上面提到的是Ember/Ember CLI的过时版本。现在我在一个mixin-中定义了这个函数

  customAjax: (method, type, id, action, hash = null) ->
    #note that you can now also get the adapter from the store -
    #adapter = @store.adapterFor('application')
    adapter = @container.lookup('adapter:application')
    url = adapter.buildURL(type, id) + '/' + action
    hash['data'] = $.extend({}, hash) if hash #because rails
    adapter.ajax(url, method, hash).then (result) =>
      return result

并称之为

@customAjax('PUT', 'modelClass', modelId, 'nonCRUDActionName', optionalHashOfAdditionalData).then (response) =>
  #do something with response

我经历了同样的问题,并使用此处解释的修改版本解决了它:Ember.js 中的非crud rails操作

基本上,您需要使用Jquery AJAX调用,对我来说,重要的部分是更改contentType,以便在请求标头中发送auth数据,而不是表单数据(默认情况下,如果您在调用中包含数据)。此外,请确保在AJAX部分提供正确的上下文,否则"this"在"success"回调中不起作用:

App.ItemController = Ember.ObjectController.extend
    actions:
        starItem: ->
            controller = this
            item = @get 'model'
            id = item.id
            token = @auth.get 'authToken'
            $.ajax
                type: 'PUT'
                context: controller
                data: '{ "auth_token": "' + token + '"}'
                url: '/api/v1/items/' + id + '/star'
                contentType: 'application/json; charset=UTF-8'
                dataType: 'json'
                success: ->
                    if @get 'item.starred_by_user'
                        @set 'model.starred_by_user', false
                        item.decrementProperty('total_stars') 
                    else 
                        @set 'model.starred_by_user', true
                        item.incrementProperty('total_stars')

也许是一个好的开始:https://github.com/beautifulnode/ember.request

还没有尝试过,但在你的情况下似乎很有希望。。。

我认为,在CQRS中,therms-star和unstar是命令。因此,您应该声明模型PersonStar并使用它。

在讨论组中,耶胡达提到这还不可能。