API Docs for: 0.2.1
Show:

Resource Class

Module: resource
Parent Module: ember-jsonapi-resources

A Resource class to create JSON API resource objects. This is abstract, first define a prototype using Resource.extend({ type: entity }). Model prototypes are registered in the container as factories, they use the options: { instantiate: false, singleton: false }. So, to create a model instance use the owner API method _lookupFactory('model:name')† then create():

let model = Ember.getOwner(this)._lookupFactory('model:entity').create({ attributes: { key: value } });

Note: eventually factoryFor will replace _lookupFactory

See http://jsonapi.org/format/#document-resource-objects

Methods

_existingRelationshipData

(
  • relation
)
private

Parameters:

  • relation String

_relationAdded

(
  • relation
  • identifier,
  • previous,
)
private

Track additions of relationships using a resource identifier objects:

{ relation {String}, type {String}, kind {String} }`

Parameters:

  • relation String

    name of a related resource

  • identifier, Object

    a resource identifier object {type: String, id: String}

  • previous, Object | Array

    resource identifier object or array of identifiers

_relationRemoved

(
  • relation
  • id
)
private

Track removals of relationships

Parameters:

  • relation String
    • resource name
  • id String

_replaceRelationshipsData

(
  • relation
  • ids
)
private

Parameters:

  • relation String
  • ids Array | String | Null

_resetAttributes

() private

Reset tracked changed/previous attrs

_resetRelationships

() private

Reset tracked relationship changes

_updateRelationshipsData

(
  • relation
  • ids
)
private

Parameters:

  • relation String
  • ids Array | String | Null

_updateToOneRelationshipData

(
  • relation
  • id
)
private

Parameters:

  • relation String
  • id String | Null

addRelationship

(
  • related
  • id
)

Adds related resource identifier object to the relationship data.

Also sets or adds to the content of the related proxy object.

  • For to-many relations the related identifier object is added to the resource linkage data array.
  • For to-one relations the resource identifier object is assigned, so the relation may be replaced.

See:

Parameters:

  • related String
    • resource name
  • id String

addRelationships

(
  • related
  • ids
)

Parameters:

  • related String
    • resource name
  • ids Array

attr

(
  • [type]
  • [mutable=true]
)
Object final

Provided by the utils module.

Defined in addon/utils/attr.js:10

Utility helper to setup a computed property for a resource attribute, imported and exported with the resource submodule.

An attr of the resource is a computed property to the actual attribute in an attributes hash on the resource (model) instance. Using attr() supports any type, and an optional type (String) argument can be used to enforce setting and getting with a specific type. 'string', 'number', 'boolean', 'date', 'object', and 'array' are all valid types for attributes.

Use attr(), with optional type argument, to compose your model attributes, e.g:

import Ember from 'ember';
import Resource from 'ember-jsonapi-resources/models/resource';
import { attr, toOne, toMany } from 'ember-jsonapi-resources/models/resource';

export default Resource.extend({
  type: 'articles',
  service: Ember.inject.service('articles'),

  title: attr('string'),
  published: attr('date'),
  tags: attr('array'),
  footnotes: attr('object'),
  revisions: attr()
  version: attr('number'),
  "is-approved": attr('boolean')
});

Parameters:

  • [type] String optional

    an optional param for the type of property, i.e. string, number, boolean, date, object, or array

  • [mutable=true] Boolean optional

    optional param, defaults to true if not passed

Returns:

Object:

computed property

changedAttributes

() Object

Returns:

Object:

the changed attributes

create

() Resource static

To protect the JSON API Resource properties for attributes, links and relationships these objects are setup during create(). This has to be defined since the attr() helper needs to have new objects for each instance, to project from keeping a reference on the prototype.

The create method should only be called after looking up a factory from the container, for example in a route's model hook:

model() {
  let owner = Ember.getOwner(this);
  return owner._lookupFactory('model:post').create({
    attributes: {
      title: 'The JSON API 1.0 Spec Rocks!'
    }
  });
}

The create method uses the container to lookup the factory's prototype and find the computed properties used for relations to setup the relationships for the Resource instance you create. Calling Resource#create without using the factory lookup will result in an instance without a reference to the application's container and you will have to manually setup the relationships object prior to adding a relationship.

Returns:

Resource:

instance with protected objects: attributes, links and relationships

didResolveProxyRelation

(
  • relation
  • kind
  • related
)

Sets the relationships data, used after the promise proxy resolves by toOne and toMany helpers

Parameters:

  • relation String

    name

  • kind String

    of relation toOne or toMany

  • related Array | Object

    resource(s)

didUpdateResource

(
  • json
)

Sets all payload properties on the resource and resets private _attributes used for changed/previous tracking

Parameters:

  • json Object

    the updated data for the resource

initialize

()

Provided by the initializers module.

Defined in addon/initializers/model-setup.js:6

Initializer for the model factories, registers option to not initialize

previousAttributes

() Object

Returns:

Object:

the previous attributes

previousAttributes

() Object

Returns:

Object:

the previous attributes

relationMetadata

(
  • property
)
Object | Undefined

Parameters:

  • property String

    name of a related resource

Returns:

Object | Undefined:

{ relation {String}, type {String}, kind {String} }

removeRelationship

(
  • related
  • id
)

Removes resource identifier object of the relationship data. Also, sets the content of the related (computed property's) proxy object to null.

  • For to-one relations the (resource linkage) data is set to null.
  • For to-many relations the resource identifier object is removed from the resource Linkage data array.

See:

Parameters:

  • related String
    • resource name
  • id String

removeRelationships

(
  • related
  • ids
)

Parameters:

  • related String
    • resource name
  • ids Array

rollback

()

Rollback changes to attributes and relationships

rollbackAttributes

()

Revert to previous attributes

rollbackRelationships

()

Revert to previous relationships

toMany

(
  • relation
)
Object final

Provided by the utils module.

Defined in addon/utils/to-many.js:12

Helper to setup a has many relationship to another resource

let Author = Resource.extend({
  type: 'authors',
  name: attr(),
  posts: toMany('posts')
});

Or, with an optional type to use instead of the resource's service

let Person = Resource.extend({
  type: 'people',
  name: attr()
});

let Supervisor = Person.extend({
  type: 'supervisors',
  directReports: toMany({ resource: 'employees', type: 'people' })
});

Parameters:

  • relation String | Object

    the name of the relationship

    • resource String

      the name of the relationship

    • type String

      the name of the type or service to use

Returns:

Object:

computed property

toOne

(
  • relation
)
Object final

Provided by the utils module.

Defined in addon/utils/to-one.js:12

Helper to setup a has one relationship to another resource

let Employee = Person.extend({
  type: 'employees',
  supervisor: toOne('supervisor')
});

Or, with an optional type to use instead of the resource's service

let Person = Resource.extend({
  type: 'people',
  name: attr()
});

let Employee = Person.extend({
  type: 'employees',
  supervisor: toOne({ resource: 'supervisor', type: 'people' })
});

Parameters:

  • relation String | Object

    the name of the relationship

    • resource String

      the name of the relationship

    • type String

      the name of the type or service to use

Returns:

Object:

computed property

toString

()

Custom toString method used for clarity that the instance is a JSON API Resource kind of object

Properties

_attributes

Object private

Hash of attributes for changed/previous values

_relationships

Object private

Hash of relationships that were changed

attributes

Object protected

An optional attributes property of for a JSON API Resource object, setup in create()

This object will keep the values from the response object and may be mutable Use this as a refence for creating computed properties

For example the attr() helper sets up a properties based on this content

cacheDuration

Number

A local cache duration, to minimize duplicate fetch requests

id

String

Persisted resource ID value

isCacheExpired

Boolean

isNew

Boolean

Flag for new instance, e.g. not persisted

meta

Object protected

An optional property of for a JSON API Resource object, setup in create()

relationships

Object protected

An optional relationships property of for a JSON API Resource object, setup in create()

type

String

Extending Prototypes Must define a type value for the entity, e.g. posts