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

Add new basic adapter and serializer. #11

Merged
merged 3 commits into from
Nov 1, 2014
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
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ Ember Django Adapter
====================

This Ember addon enables the use of [Django REST Framework][] as an API
backend. The core functionality of the adapter is in
[toranb/ember-data-django-rest-adapter][]. The addon is compatible with
[ember-cli][] version 0.1.0 and higher.
backend. The addon is compatible with [ember-cli][] version 0.1.0 and higher.

Goals
-----

* Ability to support Django REST Framework APIs without modifications to the default configuration of Django REST
Framework.

* Ensure as much as posslibe that the documentation on emberjs.com about adapters and serializers is accurate and
relevant when using the Django REST adapter and serializer. Deviations from the emberjs.com documentation should be
documented clearly.

* Optionally enable some features from Django REST Framework which would require users to setup or configure their APIs
in a specific manner. An example of this is retrieving hasMany records using query params as configured with Django
REST Framework and django-filter.


Community
Expand Down Expand Up @@ -65,12 +77,41 @@ ember generate django-adapter my-custom-adapter
ember generate django-serializer my-custom-serializer
```

For examples extending the adapter, see [the cookbook][].
## Path Customization

By default the DjangoRESTAdapter will attempt to use the pluralized lowercase model name to generate the path name. This
allows some features in Django REST Framework to work by default. If this convention is not suitable for your needs,
you can override the pathForType method.

For example, if you do not want to use the default lowercase model names and needed dashed-case instead, you would
override the pathForType method like this:

```js
App.ApplicationAdapter = DS.DjangoRESTAdapter.extend({
pathForType: function(type) {
var dasherized = Ember.String.dasherize(type);
return Ember.String.pluralize(dasherized);
}
});
```

Requests for App.User would now target /users/1. Requests for App.UserProfile would now target /user-profiles/1.


## coalesceFindRequests option

This adapter does not support the coalesceFindRequests option. The Django REST Framework does not offer easy to
configure support for N+1 query requests in the format that Ember Data uses (e.g. `GET /comments?ids[]=1&ids[]=2`)

See the Ember documentation about coalesceFindRequests for information about this option [coalesce-find-requests-option][].


## More Examples

For other examples extending the adapter, see [the cookbook][].

[Django REST Framework]: http://www.django-rest-framework.org/
[toranb/ember-data-django-rest-adapter]: https://github.com/toranb/ember-data-django-rest-adapter
[ember-cli]: http://www.ember-cli.com/
[ember-django-adapter/issues]: https://github.com/dustinfarris/ember-django-adapter/issues
[coalesce-find-requests-option]: http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_coalesceFindRequests
[the cookbook]: https://github.com/dustinfarris/ember-django-adapter/wiki/Cookbook
31 changes: 31 additions & 0 deletions addon/adapters/django-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DS.DjangoRESTAdapter = DS.RESTAdapter.extend({

defaultSerializer: "DS/djangoREST",

init: function() {
this._super();

if (this.get('coalesceFindRequests')) {
var error = "Please ensure coalesceFindRequests is not present or set to false in your adapter. " +
"This adapter does not support the coalesceFindRequests option. The Django REST Framework does not offer " +
"easy to configure support for N+1 query requests in the format that Ember Data uses " +
"(e.g. GET /comments?ids[]=1&ids[]=2)" +
"See the Ember documentation about coalesceFindRequests for information about this option:" +
"http://emberjs.com/api/data/classes/DS.RESTAdapter.html#property_coalesceFindRequests";
throw new EmberError(error);
}
},

pathForType: function(type) {
var lowerCaseType = type.toLowerCase();
return Em.String.pluralize(lowerCaseType);
},

buildURL: function(type, id, record) {
var url = this._super(type, id, record);
if (url.charAt(url.length - 1) !== '/') {
url += '/';
}
return url;
}
});
30 changes: 30 additions & 0 deletions addon/adapters/django-serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

DS.DjangoRESTSerializer = DS.RESTSerializer.extend({

extractMeta: function(store, type, payload) {
if (payload && payload.results) {
// Sets the metadata for the type.
store.metaForType(type, { total: payload.count, next: payload.next, previous: payload.previous });
// Keeps ember data from trying to parse the metadata as a records.
delete payload.count;
delete payload.next;
delete payload.previous
}
},

extractArray: function(store, primaryType, rawPayload) {
// Convert rawPayload to json format expected by the RESTSerializer. This function is being overridden instead of
// normalizePayload() because 'results' will only be in lists.
var payload = {};
payload[primaryType.typeKey] = rawPayload['results'] ? rawPayload['results'] : rawPayload;
return this._super(store, primaryType, payload);
},

keyForAttribute: function(attr) {
return Em.String.decamelize(attr);
},

keyForRelationship: function(rawKey, kind) {
return Em.String.decamelize(rawKey);
}
});
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
name: 'Ember Django Adapter',

Expand Down