All Projects → NullVoxPopuli → ember-contextual-services

NullVoxPopuli / ember-contextual-services

Licence: MIT license
Services in Ember are scoped to the app as a whole and are singletons. Sometimes you don't want that. :) This addon provides ephemeral route-based services.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
typescript
32286 projects
Handlebars
879 projects

Projects that are alternatives of or similar to ember-contextual-services

Ember Basic Dropdown
The basic dropdown you ember app needs
Stars: ✭ 140 (+600%)
Mutual labels:  ember, addon
Ember Page Title
Page title management for Ember.js Apps
Stars: ✭ 177 (+785%)
Mutual labels:  ember, addon
Ember Web App
NOTICE: official repository moved to https://github.com/zonkyio/ember-web-app
Stars: ✭ 143 (+615%)
Mutual labels:  ember, addon
Ember Attacher
Native tooltips and popovers for Ember.js
Stars: ✭ 69 (+245%)
Mutual labels:  ember, addon
ember-window-messenger
This aims to be an simple window postMessage services provider.
Stars: ✭ 17 (-15%)
Mutual labels:  ember, addon
Ember Socket Guru
Addon for easy integration with Pusher.js, ActionCable, Socket.io and Phoenix Channels
Stars: ✭ 119 (+495%)
Mutual labels:  ember, addon
Ember File Upload
HTML5 file uploads for Ember apps
Stars: ✭ 172 (+760%)
Mutual labels:  ember, addon
ember-eui
Ember Components for Elastic Eui
Stars: ✭ 22 (+10%)
Mutual labels:  ember, addon
ember-audio
An Ember addon that makes working with the Web Audio API super EZ.
Stars: ✭ 33 (+65%)
Mutual labels:  ember, addon
Ember Font Awesome
ember-cli addon for using Font Awesome icons in Ember apps
Stars: ✭ 225 (+1025%)
Mutual labels:  ember, addon
Ember Searchable Select
Data-down, actions up select-like menu with searching and tagging capabilities.
Stars: ✭ 38 (+90%)
Mutual labels:  ember, addon
ember-vertical-timeline
A Vertical Timeline for Ember.js apps 🚀
Stars: ✭ 19 (-5%)
Mutual labels:  ember, addon
Ember Cli Updater
ember-cli addon to help you update your ember-cli application or addon.
Stars: ✭ 32 (+60%)
Mutual labels:  ember, addon
Ember Data Url Templates
an ember-addon to allow building urls with url templates instead of defining buildURL
Stars: ✭ 130 (+550%)
Mutual labels:  ember, addon
Ember Can
Simple authorisation addon for Ember apps
Stars: ✭ 262 (+1210%)
Mutual labels:  ember, addon
Ember Pikaday
A datepicker component for Ember CLI projects.
Stars: ✭ 151 (+655%)
Mutual labels:  ember, addon
ember-cli-daterangepicker
Just a simple component to use bootstrap-daterangepicker.
Stars: ✭ 32 (+60%)
Mutual labels:  ember, addon
ember-links-with-follower
Render a set of links with a "follower" line underneath. The follower moves to the active link, matching size and position on the page.
Stars: ✭ 43 (+115%)
Mutual labels:  ember, addon
Ember Power Calendar
Powerful and customizable calendar component for Ember
Stars: ✭ 200 (+900%)
Mutual labels:  ember, addon
ember-osf
Ember Addon for interacting with the Open Science Framework
Stars: ✭ 14 (-30%)
Mutual labels:  ember, addon

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].