Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Mar 14, 2019
1 parent 7ccd06a commit 642df53
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 56 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,18 @@ This test application implements and showcases *a* solution for handling REST ne

> `/resource/:id/sub_resource`
It does so with an [adapter mixin](./app/mixins/sub-resource-adapter.js) overriding the `buildURL()` method.
It does so with an [application adapter](./app/adapters/application.js) overriding the `buildURL()` method.

## Proposed API

> Make your sub-resource's adapter extends the mixin.
In the sub-resource's adapter ([see example](./app/adapters/comment.js)):
```js
[...]
import SubResourceAdapterMixin from '../mixins/sub-resource-adapter';

export default ApplicationAdapter.extend(SubResourceAdapterMixin, {
[...]
});
```

> When using the adapter to query the backend, make sure to provide the `parentResource`.
In a route fetching the index of the sub-resource ([see example](./app/routes/posts/detail/comments.js)):
```js
[...]
model() {
let parentResource = this.modelFor('parent-route');
return this.store.findAll('sub-resource', { adapterOptions: { parentResource } });
return this.store.findSubAll(parentResource, 'sub-resource');
}
[...]
```
Expand All @@ -39,7 +27,7 @@ In a route fetching a single item of the sub-resource ([see example](./app/route
[...]
model({ id }) {
let parentResource = this.modelFor('parent-route');
return this.store.findRecord('sub-resource', id, { adapterOptions: { parentResource } });
return this.store.findRecord(parentResource, 'sub-resource', id);
}
[...]
```
Expand Down Expand Up @@ -117,7 +105,7 @@ By providing a reference to the parent resource, we are now able to build our su
[...]
model() {
let post = this.modelFor('posts.detail');
return this.store.findAll('comment', { adapterOptions: { parentResource: post } });
return this.store.findSubAll(post, 'comment');
}
[...]
```
Expand Down
12 changes: 12 additions & 0 deletions app/adapters/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import DS from 'ember-data';
import { get } from '@ember/object';

export default DS.RESTAdapter.extend({
buildURL(modelName, id, snapshot) {
let url = this._super(...arguments);
let parentResource = get(snapshot, 'adapterOptions.parentResource')
if (parentResource) {
let { modelName: parentModelName } = parentResource.constructor;
let parentAdapter = this.store.adapterFor(parentModelName);
let parentUrl = parentAdapter.buildURL(parentModelName, parentResource.id);
return `${parentUrl}${url}`;
}
return url;
}
});
5 changes: 0 additions & 5 deletions app/adapters/comment.js

This file was deleted.

17 changes: 0 additions & 17 deletions app/mixins/sub-resource-adapter.js

This file was deleted.

3 changes: 1 addition & 2 deletions app/models/comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import DS from 'ember-data';

export default DS.Model.extend({
content: DS.attr('string'),
post: DS.belongsTo('post')
content: DS.attr('string')
});
3 changes: 1 addition & 2 deletions app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import DS from 'ember-data';

export default DS.Model.extend({
title: DS.attr('string'),
content: DS.attr('string'),
comments: DS.hasMany('comment')
content: DS.attr('string')
});
1 change: 0 additions & 1 deletion app/routes/posts/detail/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Route from '@ember/routing/route';
export default Route.extend({
model() {
let post = this.modelFor('posts.detail');
// return this.store.findAll('comment', { adapterOptions: { parentResource: post } });
return this.store.findSubAll(post, 'comment');
}
});
1 change: 0 additions & 1 deletion app/routes/posts/detail/comments/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Route from '@ember/routing/route';
export default Route.extend({
model({ comment_id }) {
let post = this.modelFor('posts.detail');
// return this.store.findRecord('comment', comment_id, { adapterOptions: { parentResource: post } });
return this.store.findSubRecord(post, 'comment', comment_id);
}
});
22 changes: 10 additions & 12 deletions app/services/store.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import DS from 'ember-data';
import DS from "ember-data";
import { set } from '@ember/object';

const { Store } = DS;

export default DS.Store.extend({
findSubRecord(parentResource, resourceName, resourceId, options) {
options = options || {};
options.adapterOptions = options.adapterOptions || {};
Object.assign(options.adapterOptions, { parentResource });
return this.findRecord(resourceName, resourceId, options);
export default Store.extend({
findSubAll(parentResource, modelName, options = {}) {
set(options, 'parentResource', parentResource);
return this.findAll(modelName, options);
},
findSubAll(parentResource, resourceName, options) {
options = options || {};
options.adapterOptions = options.adapterOptions || {};
Object.assign(options.adapterOptions, { parentResource });
return this.findAll(resourceName, options);
findSubRecord(parentResource, modelName, id, options = {}) {
set(options, 'parentResource', parentResource);
return this.findRecord(modelName, id, options);
}
});
5 changes: 5 additions & 0 deletions mock/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"id": 1,
"content": "some comment",
"post_id": 1
},
{
"id": 2,
"content": "some comment#",
"post_id": 2
}
]
}

0 comments on commit 642df53

Please sign in to comment.