Code Monkey home page Code Monkey logo

ember-nested-resource's Introduction

nested-resources (AKA sub-resources)

TL;DR

This test application implements and showcases a solution for handling REST nested resources (AKA sub-resources).

/resource/:id/sub_resource

It does so with an adapter mixin overriding the buildURL() method.

Proposed API

Make your sub-resource's adapter extends the mixin.

In the sub-resource's adapter (see example):

[...]
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):

[...]
model() {
  let parentResource = this.modelFor('parent-route');
  return this.store.findAll('sub-resource', { adapterOptions: { parentResource } });
}
[...]

In a route fetching a single item of the sub-resource (see example):

[...]
model({ id }) {
  let parentResource = this.modelFor('parent-route');
  return this.store.findRecord('sub-resource', id, { adapterOptions: { parentResource } });
}
[...]

Example Use Case

Nested resources are available under the path of a parent resource.

Let's consider the infamous blog example:

  • a blog is a collection of posts
  • a post has comments
  • a comment has only one post

A comment only exists within the context of a post (well, at least in our dummy blog).

There is the resulting REST API contract:

GET http://localhost:3000/posts:

[
  {
    "id": 1,
    "title": "Post Title",
    "content": "Post content"
  }
]

GET http://localhost:3000/posts/1:

{
  "id": 1,
  "title": "Post Title",
  "content": "Post content"
}

GET http://localhost:3000/posts/1/comments:

[
  {
    "id": 1,
    "content": "Comment content"
  }
]

GET http://localhost:3000/posts/1/comments/1:

{
  "id": 1,
  "content": "Comment content"
}

Problem & Proposed Solution

The hard task is to be able to retrieve the comment nested-resource.

app/routes/posts/details/comments.js:

[...]
model() {
  return this.store.findAll('comment'); // this won't work, since we need the `post` parent resource to build the URL
}
[...]

The proposed solution overrides the adapter's buildURL() method to build the sub-resource URL using the parent resource URL and the sub-resource pathForType. Refer to the implementation for more details.

By providing a reference to the parent resource, we are now able to build our sub-resource URL.

app/routes/posts/detail/comments.js:

[...]
model() {
  let post = this.modelFor('posts.detail');
  return this.store.findAll('comment', { adapterOptions: { parentResource: post } });
}
[...]

Prerequisites

You will need the following things properly installed on your computer.

Installation

  • git clone <repository-url> this repository
  • cd nested-resources
  • yarn install

Running / Development

Code Generators

Make use of the many generators for code, try ember help generate for more details

Running Tests

  • yarn run json-server --watch mock/db.json --routes mock/routes.json
  • ember test --proxy http://localhost:300
  • ember test --server --proxy http://localhost:300

Linting

  • yarn lint:hbs
  • yarn lint:js
  • yarn lint:js --fix

Building

  • ember build (development)
  • ember build --environment production (production)

Deploying

Specify what it takes to deploy your app.

Further Reading / Useful Links

ember-nested-resource's People

Contributors

amessinger avatar ember-tomster avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.