Resource Class
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
Item Index
Methods
- _existingRelationshipData
- _relationAdded
- _relationRemoved
- _replaceRelationshipsData
- _resetAttributes
- _resetRelationships
- _updateRelationshipsData
- _updateToOneRelationshipData
- addRelationship
- addRelationships
- attr
- changedAttributes
- create static
- didResolveProxyRelation
- didUpdateResource
- initialize
- previousAttributes
- previousAttributes
- relationMetadata
- removeRelationship
- removeRelationships
- rollback
- rollbackAttributes
- rollbackRelationships
- toMany
- toOne
- toString
Methods
_existingRelationshipData
-
relation
Parameters:
-
relationString
_relationAdded
-
relation -
identifier, -
previous,
Track additions of relationships using a resource identifier objects:
{ relation {String}, type {String}, kind {String} }`
Parameters:
-
relationStringname of a related resource
-
identifier,Objecta resource identifier object
{type: String, id: String} -
previous,Object | Arrayresource identifier object or array of identifiers
_relationRemoved
-
relation -
id
Track removals of relationships
Parameters:
-
relationString- resource name
-
idString
_replaceRelationshipsData
-
relation -
ids
Parameters:
-
relationString -
idsArray | String | Null
_resetAttributes
()
private
Reset tracked changed/previous attrs
_resetRelationships
()
private
Reset tracked relationship changes
_updateRelationshipsData
-
relation -
ids
Parameters:
-
relationString -
idsArray | String | Null
_updateToOneRelationshipData
-
relation -
id
Parameters:
-
relationString -
idString | 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:
-
relatedString- resource name
-
idString
addRelationships
-
related -
ids
Parameters:
-
relatedString- resource name
-
idsArray
attr
-
[type] -
[mutable=true]
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 optionalan optional param for the type of property, i.e.
string,number,boolean,date,object, orarray -
[mutable=true]Boolean optionaloptional param, defaults to
trueif not passed
Returns:
computed property
changedAttributes
()
Object
Returns:
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:
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:
-
relationStringname
-
kindStringof relation toOne or toMany
-
relatedArray | Objectresource(s)
didUpdateResource
-
json
Sets all payload properties on the resource and resets private _attributes used for changed/previous tracking
Parameters:
-
jsonObjectthe updated data for the resource
initialize
()
Initializer for the model factories, registers option to not initialize
previousAttributes
()
Object
Returns:
the previous attributes
previousAttributes
()
Object
Returns:
the previous attributes
relationMetadata
-
property
Parameters:
-
propertyStringname of a related resource
Returns:
{ 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
dataarray.
See:
Parameters:
-
relatedString- resource name
-
idString
removeRelationships
-
related -
ids
Parameters:
-
relatedString- resource name
-
idsArray
rollback
()
Rollback changes to attributes and relationships
rollbackAttributes
()
Revert to previous attributes
rollbackRelationships
()
Revert to previous relationships
toMany
-
relation
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:
-
relationString | Objectthe name of the relationship
-
resourceStringthe name of the relationship
-
typeStringthe name of the type or service to use
-
Returns:
computed property
toOne
-
relation
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:
-
relationString | Objectthe name of the relationship
-
resourceStringthe name of the relationship
-
typeStringthe name of the type or service to use
-
Returns:
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
links
Object
protected
An optional links property of for a JSON API Resource object, setup in create()
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
