Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

begin integration docs #1312

Merged
merged 5 commits into from
Dec 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ This is the documentation of AMS, it's focused on the **0.10.x version.**
- [How to add root key](howto/add_root_key.md)
- [How to add pagination links](howto/add_pagination_links.md)
- [Using AMS Outside Of Controllers](howto/outside_controller_use.md)
- [How to use JSON API with Ember](howto/ember-and-json-api.md)

## Integrations
| Integration | Supported AMS versions | Gem name and/or link
|----|-----|----
| Ember.js | 0.9.x | [active-model-adapter](https://github.com/ember-data/active-model-adapter)
| Ember.js | 0.10.x + | [docs/integrations/ember-and-json-api.md](integrations/ember-and-json-api.md)
| Grape | 0.10.x + | [#1258](https://github.com/rails-api/active_model_serializers/issues/1258) |
| Grape | 0.9.x | https://github.com/jrhe/grape-active_model_serializers/ |
| Sinatra | 0.9.x | https://github.com/SauloSilva/sinatra-active-model-serializers/

## Getting Help

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# How to use JSON API Query Parameters with Ember
# Integrating with Ember and JSON API

- [Preparation](./ember-and-json-api.md#preparation)
- [Adapter Changes](./ember-and-json-api.md#adapter-changes)
- [Server-Side Changes](./ember-and-json-api.md#server-side-changes)
- [Adapter Changes](./ember-and-json-api.md#adapter-changes)
- [Serializer Changes](./ember-and-json-api.md#serializer-changes)
- [Including Nested Resources](./ember-and-json-api.md#including-nested-resources)

Expand All @@ -12,6 +13,21 @@ Note: This guide assumes that `ember-cli` is used for your ember app.
The JSON API specification calls for hyphens for multi-word separators. AMS uses underscores.
To solve this, in Ember, both the adapter and the serializer will need some modifications:

### Server-Side Changes

there are multiple mimetypes for json that should all be parsed similarly, so
in `config/initializers/mime_types.rb`:
```ruby
api_mime_types = %W(
application/vnd.api+json
text/x-json
application/json
)

Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we might want something like https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/mime_types.rb but I have no way of testing it right now

module JSONAPI
  MEDIA_TYPE = 'application/vnd.api+json'
end

Mime::Type.register JSONAPI::MEDIA_TYPE, :api_json

ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MEDIA_TYPE)] = lambda do |body|
  data = JSON.parse(body)
  data = {:_json => data} unless data.is_a?(Hash)
  data.with_indifferent_access
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could certainly add that later - maybe when the JSON API implementation becomes more complete. I think that should be separate from this PR though.

```

### Adapter Changes

```javascript
Expand All @@ -34,6 +50,8 @@ export default DS.JSONAPIAdapter.extend({

// allows queries to be sent along with a findRecord
// hopefully Ember / EmberData will soon have this built in
// ember-data issue tracked here:
// https://github.com/emberjs/data/issues/3596
urlForFindRecord(id, modelName, snapshot) {
let url = this._super(...arguments);
let query = Ember.get(snapshot, 'adapterOptions.query');
Expand Down