Giter Club home page Giter Club logo

ember-contextual-services's Introduction

ember-contextual-services

npm version CI

Eliminate Prop-Drilling by Providing Ephemeral Services based on the route!


Q: How is this different from Ember's Services?

A: Three differences:

  • it's private to the route
  • it's destroyed when you navigate away to a different route
  • primarily intended for passing the route's model data down to components without using component invocation args, which has the benefit of improving maintainability of templates, should the model's contents are structure change, because only the components in the subtree of the route that access the model data are affected. The components in between would not need to be updated as well, as they do with the default / built in patterns.

Q: Do the contextual services need to live in a specific location?

A: No, but co-location is highly encouraged. One of the main benefits to using contextual services over app-wide services is that usage can be reflected by the location of the file.

Compatibility

  • Ember.js v3.24 or above
  • Ember CLI v3.24 or above
  • Node.js v16 or above
  • ember-auto-import v2.0 or above

Installation

ember install @nullvoxpopuli/ember-contextual-services

Usage

All ContextualServices are classes that can hold any state or perform any action. They could represent specific interfaces to APIs, or a way of accessing data loaded from ember-data from deep within the route's component tree.

NOTE: all of these examples are available for viewing in the tests/dummy folder.

in routes/wherever/-contexts/local-service.js;

import { ContextualService } from '@nullvoxpopuli/ember-contextual-services';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export class LocalService extends ContextualService {
  @service router;

  @tracked foo = 0;

  @action incrementFoo() {
    this.foo++;
  }
}

in routes/wherever/some-route.js

import Route from '@ember/routing/route';
import { withContextualServices } from '@nullvoxpopuli/ember-contextual-services';

import { LocalService } from './-contexts/local-service';

@withContextualServices(LocalService)
export default class SomeRoute extends Route {

}

or if you want to utilize the route's model hook for data-loading, you could do:

@withContextualServices(PersonService)
export default class SomeRoute extends Route {
  @context(PersonService) personService;

  async model() {
    let response = await fetch('https://swapi.co/api/people/1/');
    let json = await response.json();

    this.personService.data = json;
  }
}

and finally, in a component that is rendered by your route's template or a tree of components fromy our route's template,

import Component from '@glimmer/component';

import { context } from '@nullvoxpopuli/ember-contextual-services';

import { LocalService } from 'my-app/routes/whatever/-contexts/local-service';

export default class ServiceConsumer extends Component {
  @context(LocalService) localService;
}

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

ember-contextual-services's People

Contributors

dependabot[bot] avatar ember-tomster avatar nullvoxpopuli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.