All Projects → NullVoxPopuli → ember-resources

NullVoxPopuli / ember-resources

Licence: MIT license
An implementation of Resources with some helpful utilities

Programming Languages

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

Projects that are alternatives of or similar to ember-resources

ember-data-contentful
Ember Data adapter for contentful.com
Stars: ✭ 33 (-51.47%)
Mutual labels:  ember, ember-addon
ember-validity-modifier
Ember Octane addon to add custom validity (form validation) to form fields
Stars: ✭ 28 (-58.82%)
Mutual labels:  ember, ember-addon
ember-airtable
Boilerplate for quickly prototyping apps with Airtable, Node & Ember
Stars: ✭ 21 (-69.12%)
Mutual labels:  ember, ember-addon
Ember Service Worker
A pluggable approach to Service Workers for Ember.js
Stars: ✭ 227 (+233.82%)
Mutual labels:  ember, ember-addon
ember-emojione
EmojiOne helper and components for your Ember App
Stars: ✭ 16 (-76.47%)
Mutual labels:  ember, ember-addon
Ember Lifeline
An Ember addon for managing the lifecyle of asynchronous behavior in your objects
Stars: ✭ 241 (+254.41%)
Mutual labels:  ember, ember-addon
ember-vertical-timeline
A Vertical Timeline for Ember.js apps 🚀
Stars: ✭ 19 (-72.06%)
Mutual labels:  ember, ember-addon
Ember Tooltips
Easy and extendible tooltips for Ember components - http://sir-dunxalot.github.io/ember-tooltips/
Stars: ✭ 205 (+201.47%)
Mutual labels:  ember, ember-addon
ember-right-click-menu
An easy and flexible addon to add context menus anywhere in your application
Stars: ✭ 14 (-79.41%)
Mutual labels:  ember, ember-addon
ember-cli-nouislider
{{range-slider}} component for ember-cli powered by noUiSlider
Stars: ✭ 43 (-36.76%)
Mutual labels:  ember, ember-addon
Ember Font Awesome
ember-cli addon for using Font Awesome icons in Ember apps
Stars: ✭ 225 (+230.88%)
Mutual labels:  ember, ember-addon
ember-cli-string-helpers
Set of the String helpers extracted from DockYard's ember-composable-helpers.
Stars: ✭ 73 (+7.35%)
Mutual labels:  ember, ember-addon
Ember Cli Document Title
Adding document title behaviour to your ember app
Stars: ✭ 220 (+223.53%)
Mutual labels:  ember, ember-addon
Ember Graphql Adapter
GraphQL adapter for Ember Data
Stars: ✭ 244 (+258.82%)
Mutual labels:  ember, ember-addon
Ember Models Table
Table with pagination, sorting, filtering and much more
Stars: ✭ 212 (+211.76%)
Mutual labels:  ember, ember-addon
ember-template-inspector
An ember add-on which opens the template file in the code editor while inspecting an element.
Stars: ✭ 15 (-77.94%)
Mutual labels:  ember, ember-addon
Ember Cli Notifications
⚛ Atom inspired notification messages for ember-cli
Stars: ✭ 168 (+147.06%)
Mutual labels:  ember, ember-addon
Emberx Select
Select component for Ember based on the native html select element.
Stars: ✭ 202 (+197.06%)
Mutual labels:  ember, ember-addon
ember-pipeline
Railway oriented programming in Ember
Stars: ✭ 17 (-75%)
Mutual labels:  ember, ember-addon
ember-qunit-assert-helpers
An ember-addon that provides additional QUnit 2.0 assertions specific to Ember.js.
Stars: ✭ 12 (-82.35%)
Mutual labels:  ember, ember-addon

ember-resources

npm version CI An implementation of the Resource pattern in Ember.JS.

Compatibility

  • ember-source v3.28+
  • typescript v4.5+
  • ember-auto-import v2+
  • Glint 0.8.3+
    • Note that updates to glint support will not be covered by this library's adherance to SemVer. All glint-related updates will be bugfixes until Glint is declared stable.

Installation

npm install ember-resources
# or
yarn add ember-resources
# or
ember install ember-resources

See: API Documentation for more examples.

Example (async utility)

import { trackedFunction } from 'ember-resources/util/function';

class MyClass {
  @tracked endpoint = 'starships';

  data = trackedFunction(this, async () => {
    let response = await fetch(`https://swapi.dev/api/${this.endpoint}`);
    let json = await response.json();

    return json.results;
  }),

  get records() {
    return this.data.value ?? [];
  }
}
{{this.records}}

In this example, trackedFunction will make a call to StarWars API and if endpoint changes from starships to planets, the trackedFunction will automatically re-call the StarWars API to fetch the planets.

Example (function-based resource)

Visit the docs on function-based resources.

This alternate API is more general-purpose, but has the same behavior as the above example.

import { resource, use } from 'ember-resources';
import { TrackedObject } from 'tracked-built-ins';

class MyClass {
  @tracked endpoint = 'starships';

  @use load = resource(({ on }) => {
    let state = new TrackedObject({});
    let controller = new AbortController();

    on.cleanup(() => controller.abort());

    fetch(`https://swapi.dev/api/${this.endpoint}`, { signal: controller.signal })
      .then(response => response.json())
      .then(data => {
        state.value = data;
        // ...
      })
      .catch(error => {
        state.error = error;
        // ...
      });

    return state;
  });
}
{{#if this.load.value}}
  ...
{{else if this.load.error}}
  {{this.load.error}}
{{/if}}

What is a Resource?

Resources [...] bridge a gap between imperative programming and declarative programming.

Ember templates are declarative. When we design a component [...] we are specifying declaratively the HTML that should be rendered. If the data used in the templates ever updates, then Ember will update the rendered output as well, and we don't have to worry about the details. We don't have to tell Ember which specific steps to take, and when - it figures everything out for us.

pzuraq on "Introducing @use"

So.. what is a [[Resource]], really?

A Resource is a behavior that can be used in both templates and javascript.

In JavaScript

A Resource is an alternate API for

import { cached } from '@glimmer/tracking';
import { SomeClass } from '../somewhere';

class A {
  @cached
  get myResource() {
    return new SomeClass(this.args.foo)
  }
}

In this example, myResource returns an instance of a class and will re-create that class if @foo changes. That class can have its own internal state.

This requires at least 2 imports and 4 lines of code.

In Templates

A Resource is a stateful helper:

{{#let (my-resource @foo) as |someClassInstance|}}
  {{!-- ... --}}
{{/let}}

In this example, my-resource returns an instance of a class and will re-create that class if @foo changes. That class can have its own internal state and is available for use via the local variable myResource.

This requires a stateful helper by globally available to your app. Helpers are typically stateless, so this would go against most guidance on helpers.

Debugging and working with Resources

More information contained in the docs folder.

High-fidelity sourcemaps are provided in the original typescript. However, you must be using embroider to take advantage of them. Sourcemaps should work with ember-auto-import@v2+ in non-embroider builds as well, but is untested.

Note, ember-resources is not guaranteed to be compatible with usage within @computed getters. Only auto-tracking is supported.

Related addons

List of addons that use and wrap ember-resources to provide more specific functionality:

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Thanks

This library wouldn't be possible without the work of:

So much appreciate for the work both you have put in to Resources <3


This is a V2-format Addon with V1 compatibility

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