JSPM

@ember-data/json-api

5.3.0-alpha.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 31901
  • Score
    100M100P100Q146035F
  • License MIT

Provides a JSON:API document and resource cache implementation for EmberData

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ember-data/json-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Provides an in-memory JSON:API document and resource cache implementation

    This package provides an EmberData Cache implementation for JSON:API

    Installation

    Install using your javascript package manager of choice. For instance with pnpm

    pnpm add @ember-data/json-api

    🚀 Setup

    Note When using ember-data the below configuration is handled for you automatically.

    import Store from '@ember-data/store';
    import Cache from '@ember-data/json-api';
    
    export default class extends Store {
      createCache(wrapper) {
        return new Cache(wrapper);
      }
    }

    Usage

    Usually you will directly interact with the cache only if implementing a presentation class. Below we give an example of a read-only record (mutations never written back to the cache). More typically cache interactions are something that the Store coordinates as part of the request/response lifecycle.

    import Store, { recordIdentifierFor } from '@ember-data/store';
    import Cache from '@ember-data/json-api';
    import { TrackedObject } from 'tracked-built-ins';
    
    class extends Store {
      createCache(wrapper) {
        return new Cache(wrapper);
      }
    
      instantiateRecord(identifier) {
        const { cache, notifications } = this;
        const { type, id } = identifier;
    
        // create a TrackedObject with our attributes, id and type
        const attrs = cache.peek(identifier).attributes;
        const data = Object.assign({}, attrs, { type, id });
        const record = new TrackedObject(data);
    
        // update the TrackedObject whenever attributes change
        const token = notifications.subscribe(identifier, (_, change) => {
          if (change === 'attributes') {
            Object.assign(record, cache.peek(identifier).attributes);
          }
        });
    
        // setup the ability to teardown the subscription when the
        // record is no longer needed
        record.destroy = () => {
          this.notifications.unsubscribe(token);
        };
    
        return record;
      }
    
      teardownRecord(record: FakeRecord) {
        record.destroy();
      }
    }

    For the full list of APIs available read the code documentation for EmberData Cache