Home Identifier Source Repository
import Store from 'json-api-store/src/store.js'
public class | source

Store

Static Method Summary

Static Public Methods
public static

attr(name: string, options: Object): Object

Creates a field definition for an attribute.

since 0.1.0
public static

hasMany(name: string, options: Object): Object

Creates a field definition for an has-many relationship.

since 0.1.0
public static

hasOne(name: string, options: Object): Object

Creates a field definition for an has-one relationship.

since 0.1.0

Constructor Summary

Public Constructor
public

constructor(adapter: *)

Member Summary

Public Members
public

observable: Rx.Observable

An observable that will emit events when any resource in added, updated

since 0.6.0

Method Summary

Public Methods
public

add(object: Object)

Add an individual resource to the store.

since 0.1.0
public

convert(type: string, id: string, partial: object): object

Converts the given partial into a JSON API compliant representation.

since 0.5.0
public

create(type: string, partial: Object, options: Object): Rx.Observable

Attempts to create the resource through the adapter and adds it to the

since 0.5.0
public

define(names: string | string[], definition: Object)

Defines a type of resource.

since 0.2.0
public

destroy(type: string, id: string, options: Object): Rx.Observable

Attempts to delete the resource through the adapter and removes it from

since 0.5.0
public

find(type: string, id: string): Object

Finds a resource by type and id.

since 0.1.0
public

findAll(type: string): Object[]

Finds all the resources of a given type.

since 0.7.0
public

load(type: string, id: string, options: Object): Rx.Observable

Attempts to load the given resource through the adapter and adds it to the

since 0.5.0
public

loadAll(type: string, options: Object): Rx.Observable

Attempts to load all the resources of the given type through the adapter

since 0.7.0
public

off(event: string, type: string, id: string, callback: function)

this method was deprecated. Use the store.observable property instead of this.

Unregister an event listener that was registered with on().

since 0.4.0
public

on(event: string, type: string, id: string, callback: function, context: Object)

this method was deprecated. Use the store.observable property instead of this.

Register an event listener: "added", "updated" or "removed".

since 0.4.0
public

push(root: Object)

Add a JSON API response to the store.

since 0.1.0
public

remove(type: string, id: string)

Remove a resource or collection of resources from the store.

since 0.1.0
public

update(type: string, id: string, partial: Object, options: Object): Rx.Observable

Attempts to update the resource through the adapter and updates it in the

since 0.5.0

Static Public Methods

public static attr(name: string, options: Object): Object since 0.1.0 source

Creates a field definition for an attribute.

Params:

NameTypeAttributeDescription
name string
  • optional

Name of the property to map this field from.

options Object
  • optional

An options object.

options.default string
  • optional

Default value for this field.

Return:

Object

Field definition.

public static hasMany(name: string, options: Object): Object since 0.1.0 source

Creates a field definition for an has-many relationship.

Params:

NameTypeAttributeDescription
name string
  • optional

Name of the property to map this field from.

options Object
  • optional

An options object.

options.inverse string
  • optional

Name of the inverse relationship.

Return:

Object

Field definition.

public static hasOne(name: string, options: Object): Object since 0.1.0 source

Creates a field definition for an has-one relationship.

Params:

NameTypeAttributeDescription
name string
  • optional

Name of the property to map this field from.

options Object
  • optional

An options object.

options.inverse string
  • optional

Name of the inverse relationship.

Return:

Object

Field definition.

Public Constructors

public constructor(adapter: *) source

Params:

NameTypeAttributeDescription
adapter *

Public Members

public observable: Rx.Observable since 0.6.0 source

An observable that will emit events when any resource in added, updated or removed. The object passed to listeners will be in this format:

{ name: string, type: string, id: string, resource: object }

You can learn more about RxJS observables at the GitHub repo: https://github.com/Reactive-Extensions/RxJS

Example:

let store = new Store();

store.observable.filter(e => e.name === "added").subscribe(event => {
  console.log(event.name); // "added"
  console.log(event.type); // "products"
  console.log(event.id); // "1"
  console.log(event.resource); // Map {...}
});

store.observable.filter(e => e.name === "updated").subscribe(event => {
  console.log(event.name); // "updated"
  console.log(event.type); // "products"
  console.log(event.id); // "1"
  console.log(event.resource); // Map {...}
});

store.observable.filter(e => e.name === "removed").subscribe(event => {
  console.log(event.name); // "removed"
  console.log(event.type); // "products"
  console.log(event.id); // "1"
  console.log(event.resource); // null
});

Public Methods

public add(object: Object) since 0.1.0 source

Add an individual resource to the store. This is used internally by the push() method.

Params:

NameTypeAttributeDescription
object Object
  • nullable: false

A JSON API Resource Object to be added. See: http://jsonapi.org/format/#document-resource-objects

public convert(type: string, id: string, partial: object): object since 0.5.0 source

Converts the given partial into a JSON API compliant representation.

Params:

NameTypeAttributeDescription
type string
  • optional
  • nullable: false

The type of the resource. This can be omitted if the partial includes a type property.

id string
  • optional
  • nullable: false

The id of the resource. This can be omitted if the partial includes an id property.

partial object
  • nullable: false

The data to convert.

Return:

object

JSON API version of the object.

public create(type: string, partial: Object, options: Object): Rx.Observable since 0.5.0 source

Attempts to create the resource through the adapter and adds it to the store if successful.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of resource.

partial Object
  • nullable: false

Data to create the resource with.

options Object
  • optional

Options to pass to the adapter.

Return:

Rx.Observable

Example:

let adapter = new Store.AjaxAdapter();
let store = new Store(adpater);
store.create("product", { title: "A Book" }).subscribe((product) => {
  console.log(product.title);
});

public define(names: string | string[], definition: Object) since 0.2.0 source

Defines a type of resource.

Params:

NameTypeAttributeDescription
names string | string[]
  • nullable: false

Name(s) of the resource.

definition Object
  • nullable: false

The resource's definition.

public destroy(type: string, id: string, options: Object): Rx.Observable since 0.5.0 source

Attempts to delete the resource through the adapter and removes it from the store if successful.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of resource.

id string
  • nullable: false

ID of resource.

options Object
  • optional

Options to pass to the adapter.

Return:

Rx.Observable

Example:

let adapter = new Store.AjaxAdapter();
let store = new Store(adpater);
store.destroy("product", "1").subscribe(() => {
  console.log("Destroyed!");
});

public find(type: string, id: string): Object since 0.1.0 source

Finds a resource by type and id.

NOTE: If the resource hasn't been loaded via an add() or push() call it will be automatically created when find is called.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of the resource to find.

id string
  • nullable: false

The id of the resource to find.

Return:

Object

The resource.

public findAll(type: string): Object[] since 0.7.0 source

Finds all the resources of a given type.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of the resource to find.

Return:

Object[]

An array of resources.

public load(type: string, id: string, options: Object): Rx.Observable since 0.5.0 source

Attempts to load the given resource through the adapter and adds it to the store if successful.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of resource.

id string
  • nullable: false

ID of resource.

options Object
  • optional

Options to pass to the adapter.

Return:

Rx.Observable

Example:

let adapter = new Store.AjaxAdapter();
let store = new Store(adpater);
store.load("products", "1").subscribe((product) => {
  console.log(product.title);
});

public loadAll(type: string, options: Object): Rx.Observable since 0.7.0 source

Attempts to load all the resources of the given type through the adapter and adds them to the store if successful.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of resource.

options Object
  • optional

Options to pass to the adapter.

Return:

Rx.Observable

Example:

let adapter = new Store.AjaxAdapter();
let store = new Store(adpater);
store.loadAll("products").subscribe((products) => {
  console.log(products);
});

public off(event: string, type: string, id: string, callback: function) since 0.4.0 source

this method was deprecated. Use the store.observable property instead of this.

Unregister an event listener that was registered with on().

Params:

NameTypeAttributeDescription
event string

Name of the event.

type string

Name of resource to originally passed to on().

id string
  • optional

ID of the resource to originally passed to on().

callback function

Function originally passed to on().

public on(event: string, type: string, id: string, callback: function, context: Object) since 0.4.0 source

this method was deprecated. Use the store.observable property instead of this.

Register an event listener: "added", "updated" or "removed".

Params:

NameTypeAttributeDescription
event string

Name of the event.

type string

Name of resource to watch.

id string
  • optional

ID of the resource to watch.

callback function

Function to call when the event occurs.

context Object
  • optional

Context in which to call the callback.

public push(root: Object) since 0.1.0 source

Add a JSON API response to the store. This method can be used to handle a successful GET or POST response from the server.

Params:

NameTypeAttributeDescription
root Object

Top Level Object to push. See: http://jsonapi.org/format/#document-top-level

public remove(type: string, id: string) since 0.1.0 source

Remove a resource or collection of resources from the store.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of the resource(s) to remove.

id string
  • optional

The id of the resource to remove. If omitted all resources of the type will be removed.

public update(type: string, id: string, partial: Object, options: Object): Rx.Observable since 0.5.0 source

Attempts to update the resource through the adapter and updates it in the store if successful.

Params:

NameTypeAttributeDescription
type string
  • nullable: false

Type of resource.

id string
  • nullable: false

ID of resource.

partial Object
  • nullable: false

Data to update the resource with.

options Object
  • optional

Options to pass to the adapter.

Return:

Rx.Observable

Example:

let adapter = new Store.AjaxAdapter();
let store = new Store(adpater);
store.update("product", "1", { title: "foo" }).subscribe((product) => {
  console.log(product.title);
});