Reference Source
import Model from 'flarum/Model'
public class | source

Model

The Model class represents a local data resource. It provides methods to persist changes via the API.

Static Method Summary

Static Public Methods
public static

attribute(name: String, transform: function): *

Generate a function which returns the value of the given attribute.

public static

Generate a function which returns the value of the given has-many relationship.

public static

Generate a function which returns the value of the given has-one relationship.

public static

Transform the given value into a Date object.

Static Protected Methods
protected static

Get a resource identifier object for the given model.

Constructor Summary

Public Constructor
public

constructor(data: Object, store: Store)

Member Summary

Public Members
public

The resource object from the API.

public

Whether or not the resource exists on the server.

public

The time at which the model's data was last updated. Watching the value of this property is a fast way to retain/cache a subtree if data hasn't changed.

Protected Members
protected

The data store that this resource should be persisted to.

Method Summary

Public Methods
public

attribute(attribute: String): *

Get one of the model's attributes.

public

copyData(): *

public

delete(data: Object, options: Object): Promise

Send a request to delete the resource.

public

id(): Integer

Get the model's ID.

public

pushAttributes(attributes: Object)

Merge new attributes into this model locally.

public

pushData(data: Object)

Merge new data into this model locally.

public

save(attributes: Object, options: Object): Promise

Merge new attributes into this model, both locally and with persistence.

Protected Methods
protected

Construct a path to the API endpoint for this resource.

Static Public Methods

public static attribute(name: String, transform: function): * source

Generate a function which returns the value of the given attribute.

Params:

NameTypeAttributeDescription
name String
transform function
  • optional

A function to transform the attribute value

Return:

*

public static hasMany(name: String): Array | Boolean source

Generate a function which returns the value of the given has-many relationship.

Params:

NameTypeAttributeDescription
name String

Return:

Array | Boolean

false if no information about the relationship exists; an array if it does, containing models if they have been loaded, and undefined for those that have not.

public static hasOne(name: String): Model | Boolean | undefined source

Generate a function which returns the value of the given has-one relationship.

Params:

NameTypeAttributeDescription
name String

Return:

Model | Boolean | undefined

false if no information about the relationship exists; undefined if the relationship exists but the model has not been loaded; or the model if it has been loaded.

public static transformDate(value: String): Date | null source

Transform the given value into a Date object.

Params:

NameTypeAttributeDescription
value String

Return:

Date | null

Static Protected Methods

protected static getIdentifier(model: Model): Object source

Get a resource identifier object for the given model.

Params:

NameTypeAttributeDescription
model Model

Return:

Object

Public Constructors

public constructor(data: Object, store: Store) source

Params:

NameTypeAttributeDescription
data Object

A resource object from the API.

store Store

The data store that this model should be persisted to.

Public Members

public data: Object source

The resource object from the API.

public exists: Boolean source

Whether or not the resource exists on the server.

public freshness: Date source

The time at which the model's data was last updated. Watching the value of this property is a fast way to retain/cache a subtree if data hasn't changed.

Protected Members

protected store: Store source

The data store that this resource should be persisted to.

Public Methods

public attribute(attribute: String): * source

Get one of the model's attributes.

Params:

NameTypeAttributeDescription
attribute String

Return:

*

public copyData(): * source

Return:

*

public delete(data: Object, options: Object): Promise source

Send a request to delete the resource.

Params:

NameTypeAttributeDescription
data Object

Data to send along with the DELETE request.

options Object
  • optional

Return:

Promise

public id(): Integer source

Get the model's ID.

Return:

Integer

public pushAttributes(attributes: Object) source

Merge new attributes into this model locally.

Params:

NameTypeAttributeDescription
attributes Object

The attributes to merge.

public pushData(data: Object) source

Merge new data into this model locally.

Params:

NameTypeAttributeDescription
data Object

A resource object to merge into this model

public save(attributes: Object, options: Object): Promise source

Merge new attributes into this model, both locally and with persistence.

Params:

NameTypeAttributeDescription
attributes Object

The attributes to save. If a 'relationships' key exists, it will be extracted and relationships will also be saved.

options Object
  • optional

Return:

Promise

Protected Methods

protected apiEndpoint(): String source

Construct a path to the API endpoint for this resource.

Return:

String