Store
Static Method Summary
Static Public Methods | ||
public static |
Creates a field definition for an attribute. |
since 0.1.0 |
public static |
Creates a field definition for an has-many relationship. |
since 0.1.0 |
public static |
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 an individual resource to the store. |
since 0.1.0 |
public |
Converts the given partial into a JSON API compliant representation. |
since 0.5.0 |
public |
Attempts to create the resource through the adapter and adds it to the |
since 0.5.0 |
public |
Defines a type of resource. |
since 0.2.0 |
public |
Attempts to delete the resource through the adapter and removes it from |
since 0.5.0 |
public |
Finds a resource by type and id. |
since 0.1.0 |
public |
Finds all the resources of a given type. |
since 0.7.0 |
public |
Attempts to load the given resource through the adapter and adds it to the |
since 0.5.0 |
public |
Attempts to load all the resources of the given type through the adapter |
since 0.7.0 |
public |
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 |
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 |
Add a JSON API response to the store. |
since 0.1.0 |
public |
Remove a resource or collection of resources from the store. |
since 0.1.0 |
public |
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.
public static hasMany(name: string, options: Object): Object since 0.1.0 source
Creates a field definition for an has-many relationship.
Public Constructors
public constructor(adapter: *) source
Params:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
object | Object |
|
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:
Name | Type | Attribute | Description |
type | string |
|
The type of the resource. This can be omitted if the partial includes a type property. |
id | string |
|
The id of the resource. This can be omitted if the partial includes an id property. |
partial | object |
|
The data to convert. |
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.
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.
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.
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.
public findAll(type: string): Object[] since 0.7.0 source
Finds all the resources of a given type.
Params:
Name | Type | Attribute | Description |
type | string |
|
Type of the resource to find. |
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.
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.
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
store.observable
property instead of this.Unregister an event listener that was registered with on().
public on(event: string, type: string, id: string, callback: function, context: Object) since 0.4.0 source
store.observable
property instead of this.Register an event listener: "added", "updated" or "removed".
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:
Name | Type | Attribute | Description |
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.
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.
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);
});