Manual Reference Source Test
import ProjectContent from 'clinical6/src/helpers/ProjectContent.js'
public class | source

ProjectContent

The ProjectContent class wraps custom API endpoints in a simple and quick set of objects / methods. This object will contain many properties belonging to the type when returned from the server. A ProjectContent for a car, for example, may have myCar.make or myCar.model in addition to the listed properties.

Example:

let labs1 = new ProjectContent('site_start/labs');  // commonly used
let labs2 = new ProjectContent('site_start/labs', { version: 'v2' });
let labs3 = new ProjectContent('site_start/labs', { id: 23 });  // commonly used
let labs4 = new ProjectContent('site_start/labs', {}, { make: 'corolla' });

Constructor Summary

Public Constructor
public

constructor(type: String, options: any, data: any)

Creates an instance of ProjectContent.

Member Summary

Public Members
public get

Return meta data for the project content.

public set

This is used to manage the _options property

  • owner (unique identifier / id)
  • ownerType (content type)
  • version
public get

This is used to manage the _options property

Private Members
private set

Set _data to set information to the current object.

private get

Get _data to quickly get the raw data without options.

private
private

Method Summary

Public Methods
public

addChild(type: String, child: Object): Promise<Object>

Add child of type belonging to the ProjectContent

public

Gets all data belonging to the ProjectContent ownerType

public

Get one child with id and type belonging to the ProjectContent

public

Get all children with type belonging to the ProjectContent

public

Gets data belonging to this ProjectContent (ownerType and owner).

public

Get data belonging to a sibling provided the id assuming ProjectContent's type

public

removeChild(type: String, id: Number): Promise<any>

Remove child of type belonging to the ProjectContent

public

save(): Promise<any>

Saves the current ProjectContent object.

public

updateChild(type: String, child: Object, id: Number): Promise<any>

Update child of type belonging to the ProjectContent

Public Constructors

public constructor(type: String, options: any, data: any) source

Creates an instance of ProjectContent.

Params:

NameTypeAttributeDescription
type String
  • optional
  • default: ''
  • nullable: true

Type of the ProjectContent ('site_start/labs')

options any
  • optional
  • default: {}
  • nullable: true

Options available to support the API. Version = 'v2'

data any
  • optional
  • default: {}
  • nullable: true

The data of the object

Public Members

public get meta: Object source

Return meta data for the project content. The data it consumes is required to come at the root of the API endpoint with the meta key containing keys pointing to arrays of data. For example {"meta": { "certificate_types": [ {"value": ..., "label": ...} ] }} could then be obtained through myProjectContent.meta['certificate_types']

public set options: Object source

This is used to manage the _options property

  • owner (unique identifier / id)
  • ownerType (content type)
  • version

public get options: Object source

This is used to manage the _options property

Private Members

private set _data: Object source

Set _data to set information to the current object.

private get _data: Object source

Get _data to quickly get the raw data without options.

private _meta: Object source

private _options: Object source

Public Methods

public addChild(type: String, child: Object): Promise<Object> source

Add child of type belonging to the ProjectContent

Params:

NameTypeAttributeDescription
type String

The type of the child being inserted

child Object

The child data being inserted

Return:

Promise<Object>

Promise of the resulting API call

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let lab_director = { first_name: 'Joe', last_name: 'Waldron' };
await lab.addChild('lab_directors', lab_director).then((data) => (lab_director = data));

Test:

public getAll(): Promise<ProjectContent[]> source

Gets all data belonging to the ProjectContent ownerType

Return:

Promise<ProjectContent[]>

Promise of an Array of ProjectContent

Example:

let labs = new ProjectContent('site_start/labs');
labs.getAll().then((list) => {
  console.log(list);
});
// or alternatively in one line
(new ProjectContent('site_start/labs')).getAll().then((list) => console.log(list));

public getChild(type: String, id: Number): Promise<Object> source

Get one child with id and type belonging to the ProjectContent

Params:

NameTypeAttributeDescription
type String

The type of the child

id Number

The id of the child

Return:

Promise<Object>

Promise of the resulting API call

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let lab_director = await lab.getChild('lab_directors', 512);

Test:

public getChildren(type: String): Promise<Array> source

Get all children with type belonging to the ProjectContent

Params:

NameTypeAttributeDescription
type String

The type of the children to be returned

Return:

Promise<Array>

Promise of an array of children json

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let lab_directors = await lab.getChildren('lab_directors');

Test:

public getData(): Promise<Object> source

Gets data belonging to this ProjectContent (ownerType and owner). Stores data in ProjectContent.

Return:

Promise<Object>

Promise of the data

Example:

let lab = new ProjectContent('site_start/labs');
lab.getAll().then((list) => {
  list[0].getData().then(() => console.log(list[0])); // should have new properties
});
lab = new ProjectContent('site_start/labs', { id: 23 });
lab.getData().then(() => console.log(lab)); // should have new properties
lab.meta;  // Accumulates through getData, getChildren, and getSibling calls

Test:

public getSibling(id: Number): Promise<ProjectContent> source

Get data belonging to a sibling provided the id assuming ProjectContent's type

Params:

NameTypeAttributeDescription
id Number

The id of the sibling

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let other_lab = await lab.getSibling(24);

public removeChild(type: String, id: Number): Promise<any> source

Remove child of type belonging to the ProjectContent

Params:

NameTypeAttributeDescription
type String

The type of the child being removed

id Number

The child data being inserted

Return:

Promise<any>

Promise of the resulting API call

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let lab_director = await lab.removeChild('lab_directors', 512);

Test:

public save(): Promise<any> source

Saves the current ProjectContent object. If it has an id, then update else insert.

Return:

Promise<any>

Example:

// Create new lab
let lab = new ProjectContent('site_start/labs');
lab.name = 'La Jolla Clinic';
await lab.save();  // id does not exist so it inserts, but will now have name
// Update existing lab name
lab.name = 'New Name';
lab.save();

public updateChild(type: String, child: Object, id: Number): Promise<any> source

Update child of type belonging to the ProjectContent

Params:

NameTypeAttributeDescription
type String

The type of the child being updated

child Object

The child data being updated

id Number
  • optional

The id of the child being updated

Return:

Promise<any>

Example:

let lab = new ProjectContent('site_start/labs', { id: 23 });
let lab_directors = await lab.getChildren('lab_directors');
lab_directors[0].first_name = 'Joe';
lab.updateChild('lab_directors', lab_directors[0]);

Test: