How to adapt "adapter" and "store" to fit my "almost" REST api?

First I would like to say, thank you for such a great framework. I am new to Ember JS, I’m using it since 4th of may 2015. The project in which I am one and only front-end developer is a combination of .NET API, two mobile (iOS and Android) thin clients and one Web thick client (which of course I have to develop).

In this project, we have at the beginning following controllers(.netters call them resources): Base, Bug, KnowledgeBase, Notifications, RefreshTokens, Roles. I am sure that it will be much more of them. The thing is, that URIs do not adhere to DS.RESTAdapter, nor to the Ember-data at all. For example here is the list of URIs for ‘Account’ resource:

Account Operations POST /api/v1/account/register POST /api/v1/account/edit POST /api/v1/account/delete GET /api/v1/account/user GET /api/v1/account/get/{id} POST /api/v1/account/changepassword

They are not moot, I can’t make them more ‘emberish’, it is not my duty. Taking this URI for example: “GET /api/v1/account/user” - it has no :id, it just gets account data of current logged in user based on the “Authorization” header used in Ajax GET call to /api/v1/account/user. Header keeps a token like this “hg7mCD4ii8cTr0hpUjSPRx6UlQsD_mEhR…”.

I managed to read some tutorials (Evil Trout), watch some videocasts and I finally did remove ember-data from project (npm rm ember-data --save-dev). I remove it because I couldn’t manage to resolve at once:

  1. Ember-data incompatibility with our API URIs
  2. Ember-cli’s convention over configuration (ember-cli is what I’m using for this project)
  3. Ajaxmockups with NodeJS Express and constant CORS exceptions showing in Chrome’s DevTools.

Then I developed store and adapter in the way described here: http://blog.bndw.org/building-a-custom-store-and-adapter-in-ember-cli/

Now I am stuck with few unknowns:

  1. Do I have hold to the method names used in Ember-data Adapters (_buildURL ajaxError ajaxSuccess buildURL createRecord deleteRecord find findBelongsTo findHasMany findMany updateRecord urlForCreateRecord…) to get my custom adapter work properly with Ember JS.
  2. Does my adapter methods has to have the same parameters (eg. find (store, type, id, snapshot)), buildURL (modelName, id, snapshot, requestType, query)
  3. How to manage Ember.$.ajax headers for inbound (same domain) requests and CORS? Eg: when calling outside API with JSON Ajax call with header Authoriazion set with token, I got 40X errors…
  4. Does store has to be one in whole application? Can it use multiple adapters (one adapter for each APIs Resource (URI))?

Thanks for reading, sorry about my english. Best regards!

Hi Marak,

I’m also new to ember and recently went through a learning experience similar to what you’re going through. The two most important lessons I learned were 1) ember-data does a lot for you and plays well with the rest of ember, and 2) you can do everything you need to do in adapters and serializers, one of each for each model.

The key concept to keep in mind is that ds.store expects certain functionality from your adapters and serailizers but it doesn’t care how you provide that functionality.

For the adapter you must provide find, findAll, etc as documented here. Implement those however you like, using jquery ajax for example. Take care to return promises when the store expects promises to be returned.

BuildUrl, urlForCreateRecord and other items not specified in DS.Adapter are required for the RESTAdapter. You may or may not want to have similar methods, but those are implementation issues, DS.Store doesn’t care.

Likewise implement the functionality required from DS.Serializer.

With regard to your #3, CORS can be a PITA when developing. I simply disable that security feature in Chrome while developing:

open -a Google\ Chrome --args --disable-web-security

Ember can be a bit confusing at first but I just keep reminding myself that whenever I start to fight the framework it’s almost always because haven’t understood something about the way Ember works and is intended to be used.

HTH!

Zane

Thanks for Your reply! I thought no one will respond because I wrote TL;DR; type of post :wink: so I flagged it ‘off topic’. Yesterday we had a meeting, and I managed to induce .NET developers to change API interface. Now I have to make a list with URIs and it will be mapped to C# controllers and methods.