Skip to content
Open
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
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.findSubRecord(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')
});
2 changes: 1 addition & 1 deletion app/routes/posts/detail/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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');
}
});
2 changes: 1 addition & 1 deletion app/routes/posts/detail/comments/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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);
}
});
15 changes: 15 additions & 0 deletions app/services/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import DS from "ember-data";
import { set } from '@ember/object';

const { Store } = DS;

export default Store.extend({
findSubAll(parentResource, modelName, options = {}) {
set(options, 'parentResource', parentResource);
return this.findAll(modelName, 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
}
]
}
12 changes: 12 additions & 0 deletions tests/unit/services/store-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Service | store', function(hooks) {
setupTest(hooks);

// Replace this with your real tests.
test('it exists', function(assert) {
let service = this.owner.lookup('service:store');
assert.ok(service);
});
});