Resource

Resource

Resource instances behave like AngularJS' ngResource, in that resources are returned directly from calls, and the values in the resource will be merged into the object once the background request(s) complete. Doing so allows a view layer to directly bind to the resource fields. Should you need to do something once the resource is loaded, the $promise property of the resource is available.

Resource offers several functions you can use to interact with links, embedded resources, and forms included in the resource.

Constructor

new Resource()

Source:

Members

$error

If there is a problem resolving the Resource, this will contain the error information.

Source:

$formatSpecific :Object

Object containing any format specific properties that don't fall under the standard categories such as forms, fields, or links.

Type:
  • Object
Source:

(nullable) $parent :Resource

For embedded/sub-resources, this will point to the immediate parent resource containing this one.

Type:
Source:

$promise :Promise

This property is a ES6 promise that can be used to perform work once the resource is resolved. For resources that were embedded, the promise may already resolved when the resource is initially created.

Type:
  • Promise
Source:

$resolved :boolean

This property is a simple boolean true/false value indicating whether the specific resource has been resolved or not.

Type:
  • boolean
Source:

(nullable) $response :Object

This property will be populated by the HTTP response information when the resource is resolved. For embedded resources, the data portion will be the subsection of the response used to created the embedded resource.

Type:
  • Object
Source:

Methods

$curiePrefix(curiePrefix) → {CuriePrefix}

Locate a media-type specific registered CURIE (compact URI) prefix (CuriePrefix).

Parameters:
Name Type Description
curiePrefix String

The CURIE prefix for look up.

Source:
Throws:

Raises an error when looking for an unknown CURIE prefix.

Type
Error
Returns:

The media-type specific CURIE prefix.

Type
CuriePrefix

$delete() → {Resource}

Send an HTTP DELETE request to the resource's 'self' link.

Source:
Returns:

A resources with the response of the DELETE request.

Type
Resource

$embedded()

Alias for Resource#$sub.

Source:

$embeddeds()

Alias for Resource#$subs.

Source:

$expandCurie(curie) → {String}

Expand a CURIE (compact URI) by looking up a prefix binding and processing it according to the media type specific CURIE processing rules.

Parameters:
Name Type Description
curie String

The compact URI to expand.

Source:
Throws:

Raises an error when trying to expand using an unknown CURIE prefix.

Type
Error
Returns:

The CURIE expanded into a final URI.

Type
String

$followAll(rel, optionsopt) → {Array}

Follow all links for the given relation and return an array of resources. If the link relation is not present, then an empty array will be returned. It will first attempt to locate the link relation in the embedded resources, and fall back to checking for the presence of a link and loading those. Depending on whether an embedded version is found, or only links, will determine whether the resources will already be resolved, or will be so in the future.

Parameters:
Name Type Attributes Description
rel string

The link relation to follow.

options Object <optional>

Options for following the link. For details, see WebLink#follow.

Properties
Name Type Attributes Description
linkFilter Object | Resource~linkPredicate <optional>

Filter object/predicate for filtering candidate links to follow.

subFilter Object | Resource~resourcePredicate <optional>

A matching object or filter function when inspecting sub/embedded resources.

Source:
Returns:

The linked/embedded resources, or an empty array if the link relation is not found.

Type
Array
Example
res.$followAll('item')
=> [Resource { $resolved: false, $promise: $q promise object }, Resource { $resolved: false, $promise: $q promise object }]

$followCurie(curie, options) → {Resource}

Expand a CURIE (compact URI) by looking up a prefix binding and processing it according to the media type specific CURIE processing rules, and then follow the final URI.

Parameters:
Name Type Description
curie String

The compact URI to follow

options Object

The options to pass when following the expanded URI.

Source:
Throws:

Raises an error when looking for an unknown CURIE prefix.

Type
Error
Returns:

The resource from following the expanded URI.

Type
Resource

$followOne(rel, optionsopt) → {Resource}

Follows a link relation, if present. The link relation will be looked for in the embedded resources first, and fall back to checking for the presence of a link and loading those. Depending on whether an embedded version is found, or only a link, will determine whether the resource will already be resolved, or will be so in the future. The optional options parameter can be used to pass additional options to the underlying http request.

Parameters:
Name Type Attributes Description
rel string

The link relation to follow.

options Object <optional>

Options for following the link. For details, see WebLink#follow.

Properties
Name Type Attributes Description
linkFilter Object | Resource~linkPredicate <optional>

A matching object or filter function when inspecting links.

subFilter Object | Resource~resourcePredicate <optional>

A matching object or filter function when inspecting sub/embedded resources.

Source:
Throws:

Will throw an error if multiple instances of the relation are present.

Returns:

The linked/embedded resource, or null if the link relation is not found.

Type
Resource
Example
res.$followOne('next')
=> Resource { $resolved: false, $promise: $q promise object }

$form(rel) → {Form}

Get the single Form for the given relation. The returned form is a cloned copy of the Form in the resource. Each call to this function will return a new copy, so that multiple forms can be created, modified, and submitted without reloading the containing Resource.

Parameters:
Name Type Description
rel string

The link relation to look up.

Source:
Throws:

An error if multiple forms are present for the link relation.

Returns:

The copy of form with the given link relation, or null if not found.

Type
Form
Example
res.$form('create-form')
=> Form { href: '/posts?page=2', method: 'POST', ... }

$forms(relopt) → {Array}

Get the Form instances for the given relation. The returned forms are a cloned copy of the Form instances in the resource. Each call to this function will return new copies, so that multiple forms can be created, modified, and submitted without reloading the containing Resource.

Parameters:
Name Type Attributes Description
rel string <optional>

The link relation to look up. If omitted, returns all forms in the resource.

Source:
Returns:

An array of cloned forms, or an empty array if not found.

Type
Array
Examples
res.$forms('create-form')
=> [Form { href: '/posts?page=2', method: 'POST', ... }]
res.$forms()
=> [Form { href: '/posts?page=2, 'method: 'POST", ... }]

$has(rel, filteropt) → {boolean}

Check for existence of a linked or embedded resource for the given link relation. The function does not take into account whether the resource is resolved or not, so the return value may be different once the resource is resolved.

Parameters:
Name Type Attributes Description
rel string

The link relation to check for.

filter Object <optional>

The link/sub-resource filter

Properties
Name Type Attributes Description
linkFilter Object | Resource~linkPredicate <optional>

A matching object or filter function when inspecting links.

subFilter Object | Resource~resourcePredicate <optional>

A matching object or filter function when inspecting sub/embedded resources.

Source:
Returns:

True if the link relation is found in links or embedded, otherwise false.

Type
boolean

Get the single WebLink for the given relation.

Parameters:
Name Type Attributes Description
rel string

The link relation to look up.

filter Object | Resource~linkPredicate <optional>

The filter object/predicate to filter links to desired one.

Source:
Throws:

An error if multiple links are present for the link relation.

Returns:

The link with the given link relation, or null if not found.

Type
WebLink
Example
res.$link('next')
=> WebLink { href: '/posts?page=2' }

Return a LinkCollection for the given link relation.

Parameters:
Name Type Attributes Description
rel string <optional>

The link relation to look up. If not provided, all links in the resource will be return.

filter Object | Resource~linkPredicate <optional>

The filter object/predicate to filter matching links.

Source:
Returns:

The links with the given link relation, or all the links in the resource if a rel is not provided.

Type
LinkCollection
Example
res.$links('posts')
=> LinkCollection [ WebLink { href: '/posts/123' }, WebLink { href: '/posts/345' } ]

$sub(rel, filter}) → {Resource}

Look up the embedded/sub resource for the given link relation.

Parameters:
Name Type Description
rel string

The link relation to follow.

filter} Object | Resource~resourcePredicate

The matching object/predicate to filter sub-resources.

Source:
Throws:

Will throw an error if multiple instances of the relation are present.

Returns:

The embedded resource, or null if the link relation is not found.

Type
Resource
Example
res.$sub('item')
=> Resource { $resolved: true, $promise: resolved $q promise, ... various properties }

$subs(rel, filteropt) → {Array}

Look up the embedded/sub resources for the given link relation.

Parameters:
Name Type Attributes Description
rel string

The link relation to follow.

filter Object | Resource~resourcePredicate <optional>

A match object/predicate to limit returned sub-resources.

Source:
Returns:

Array of embedded resources, or empty array if the link relation is not found.

Type
Array
Example
res.$subs('item')
=> [Resource { $resolved: true, $promise: resolved $q promise, ... various properties }]

Type Definitions

linkPredicate(link) → {boolean}

A predicate to inspect a given Resource to decide to include or not.

Parameters:
Name Type Description
link WebLink

The candidate link

Source:
Returns:

Whether to include the link in the response(s) or not.

Type
boolean

resourcePredicate(resource) → {boolean}

A predicate to inspect a given Resource to decide to include or not.

Parameters:
Name Type Description
resource Resource

The candidate resource

Source:
Returns:

Whether to include the resource in the response(s) or not.

Type
boolean