简体   繁体   English

如何在余烬中使用适配器

[英]How to use an adapter in ember

I am trying to send data from my front end ember app to a rails API. 我正在尝试将数据从前端余烬应用程序发送到Rails API。 The method in question, donate, sits at a custom route: 有问题的方法捐赠,位于自定义路线:

patch 'accounts/:id/donate' => 'accounts#donate'

In order to call to it, I understand that my ember route needs to use an adapter to override the normal patch methods, but that is about where my knowledge ends. 为了对其进行调用,我知道我的余烬路由需要使用适配器来覆盖常规的修补方法,但这就是我的知识的终点。 This is the state of my current route: 这是我当前路线的状态:

import Ember from 'ember';

export default Ember.Route.extend({

  actions: {
    donate(params){
      console.log('hits');
      console.log(params);
      let patron = this.get('store').findRecord('account', 10);
      // here is where I need to link to my adapter
      patron.donate(params);
    }
  }
});

and my adapter file (I cannot even get this to fire yet, so the low quality of the code is to be expected): 和我的适配器文件(我什至无法启动它,因此代码质量会很低):

import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
  // Not super confident about this adapter, not am I super confident about how I
  // am passing data through it.
  donate(store, type, record) {
    let api = this.get('host');
    let serialized = this.serialize(record, {includeId: true});
    let accountId = serialized.account_id;
    let url = `${api}/accounts/${accountId}/donate`;
    let data = {account: serialized};
    return this.ajax(url, 'PATCH', {data});
  }
});

The first step to getting this functionality working is making sure my adapter can properly call to my server. 使此功能正常工作的第一步是确保适配器可以正确调用服务器。 How/where would I go about making this happen? 我将如何/在何处实现这一目标?

As you have discovered, Ember's default adapter assumes a very specific REST API resource model ( JSON API spec ). 您已经发现,Ember的默认适配器假定一个非常特定的REST API资源模型( JSON API规范 )。 However, the nice thing about Ember's community is if there's a common problem, there is often a library out there to help and it's also quite common to have an "actions" based REST API. 但是,关于Ember社区的好处是,如果存在一个常见问题,通常会有一个库可以提供帮助,并且具有基于“动作”的REST API也很常见。

tl;dr if you can add another library to your codebase, checkout ember-api-actions . tl; dr(如果可以在代码库中添加另一个库),请检出ember-api-actions From a cursory view, it seems to do exactly what you need with very little configuration. 从粗略的角度看,它几乎不需要任何配置即可完全满足您的需求。 It also has a very good rating on Ember Observer (it seems production worthy). 它在Ember Observer上也有很好的评级(似乎值得生产)。

In your route's actions, the donate action will call the createRecord method of the store object like this: 在您的路线操作中,捐赠操作将调用商店对象的createRecord方法,如下所示:

this.store.createRecord('modelName', objectToBeSentToTheServer);

In your adapter, you will need to create a createRecord function that will handle the call from the route. 在您的适配器中,您将需要创建一个createRecord函数来处理来自路由的调用。

Look at the Ember Data API: 查看Ember Data API:

https://www.emberjs.com/api/data/ https://www.emberjs.com/api/data/

The following link gives you details about the adapter functions for creating, updating, finding records: 以下链接为您提供有关用于创建,更新和查找记录的适配器功能的详细信息:

https://www.emberjs.com/api/data/classes/DS.JSONAPIAdapter.html https://www.emberjs.com/api/data/classes/DS.JSONAPIAdapter.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM