Class: Record

Record


new Record(props, opts)

js-data's Record class.

import {Record} from 'js-data'
Method parameters:
Name Type Argument Description
props Object <optional>

The initial properties of the new Record instance.

opts Object <optional>

Configuration options.

Properties
Name Type Argument Default Description
noValidate Boolean <optional>
false

Whether to skip validation on the initial properties.

Details
Source
Record.js, line 16

Extends

This class extends the Component class.

Members


_listeners

Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.

Details
Type Since Source
Object 3.0.0 Component.js, line 7
Inherited From:

Methods


afterLoadRelations(relations, opts)

TODO

Method parameters:
Name Type Description
relations Array.<String>

TODO

opts Object

TODO

Details
Source
Record.js, line 71

beforeLoadRelations(relations, opts)

TODO

Method parameters:
Name Type Description
relations Array.<String>

TODO

opts Object

TODO

Details
Source
Record.js, line 81

changes(opts)

Return changes to this record since it was instantiated or Record#commit was called.

Method parameters:
Name Type Argument Description
opts <optional>

Configuration options.

Properties
Name Type Argument Description
equalsFn Function <optional>

Equality function. Default uses ===.

ignore Array <optional>

Array of strings or RegExp of fields to ignore.

Details
Source
Record.js, line 91

commit()

TODO

Details
Source
Record.js, line 107

dbg(args)

Log the provided values at the "debug" level.

Method parameters:
Name Type Argument Description
args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 32
Inherited From:

destroy(opts)

Call Mapper#destroy using this record's primary key.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options passed to Mapper#destroy.

Return value:
Type Description
Promise

The result of calling Mapper#destroy.

Details
Source
Record.js, line 120

emit(event, args)

Trigger an event on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to emit.

args * <optional>
<repeatable>

Arguments to pass to any listeners.

Details
Since Source
3.0.0 Component.js, line 98
Inherited From:
Examples
collection.on('foo', (msg) => {
  console.log(msg) // "bar"
})
collection.emit('foo', 'bar')
store.on('foo', (msg, val1, val2) => {
  console.log(msg, val1, val2) // "bar" "beep" "boop"
})
store.emit('foo', 'bar', 'beep', 'boop')

get(key)

Return the value at the given path for this instance.

Method parameters:
Name Type Description
key String

Path of value to retrieve.

Return value:
Type Description
*

Value at path.

Details
Source
Record.js, line 135

hasChanges(opts)

Return whether this record has changed since it was instantiated or Record#commit was called.

Method parameters:
Name Type Argument Description
opts <optional>

Configuration options.

Properties
Name Type Argument Description
equalsFn Function <optional>

Equality function. Default uses ===.

ignore Array <optional>

Array of strings or RegExp of fields to ignore.

Details
Source
Record.js, line 147

hashCode()

TODO

Details
Source
Record.js, line 163

loadRelations(relations, opts)

TODO

Method parameters:
Name Type Argument Description
relations Array.<String> <optional>

TODO

opts Object <optional>

TODO

Details
Source
Record.js, line 179

log(level, args)

Log the provided values. By default sends values to console[level].

Method parameters:
Name Type Argument Description
level String

Log level

args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 39
Inherited From:

off(event, listener)

Remove an event listener from this Component. If no listener is provided, then all listeners for the specified event will be removed. If no event is specified then all listeners for all events will be removed.

Method parameters:
Name Type Argument Description
event String <optional>

Name of event to unsubsribe to.

listener Function <optional>

Listener to remove.

Details
Since Source
3.0.0 Component.js, line 79
Inherited From:
Examples

Remove a listener to a single event

collection.off('add', handler)

Remove all listeners to a single event

record.off('change')

Remove all listeners to all events

store.off()

on(event, listener, ctx)

Register a new event listener on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to subsribe to.

listener Function

Listener function to handle the event.

ctx * <optional>

Optional content in which to invoke the listener.

Details
Since Source
3.0.0 Component.js, line 49
Inherited From:
Examples

Listen for all "afterCreate" events in a DataStore

store.on('afterCreate', (mapperName, props, opts, result) => {
  console.log(mapperName) // "post"
  console.log(props.id) // undefined
  console.log(result.id) // 1234
})
store.create('post', { title: 'Modeling your data' }).then((post) => {
  console.log(post.id) // 1234
})

Listen for the "add" event on a collection

collection.on('add', (records) => {
  console.log(records) // [...]
})

Listen for "change" events on a record

post.on('change', (record, changes) => {
  console.log(changes) // { changed: { title: 'Modeling your data' } }
})
post.title = 'Modeling your data'

previous(key)

TODO

Method parameters:
Name Type Argument Description
key String <optional>

TODO

Details
Source
Record.js, line 267

revert(opts)

TODO

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options.

Details
Source
Record.js, line 282

save(opts, opts)

Delegates to Mapper#create or Mapper#update.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options. See Mapper#create.

opts <optional>

Configuration options.

Properties
Name Type Argument Description
changesOnly Boolean <optional>

Equality function. Default uses ===.

equalsFn Function <optional>

Passed to Record#changes when changesOnly is true.

ignore Array <optional>

Passed to Record#changes when changesOnly is true.

Return value:
Type Description
Promise

The result of calling Mapper#create or Mapper#update.

Details
Source
Record.js, line 308

set(key, value, opts)

Set the value for a given key, or the values for the given keys if "key" is an object.

Method parameters:
Name Type Argument Description
key String | Object

Key to set or hash of key-value pairs to set.

value * <optional>

Value to set for the given key.

opts Object <optional>

Optional configuration.

Properties
Name Type Argument Default Description
silent Boolean <optional>
false

Whether to trigger change events.

Details
Source
Record.js, line 341

toJSON(opts)

Return a plain object representation of this record. If the class from which this record was created has a mapper, then Mapper#toJSON will be called instead.

Method parameters:
Name Type Argument Description
opts Object <optional>

Configuration options.

Properties
Name Type Argument Description
with Array.<String> <optional>

Array of relation names or relation fields to include in the representation. Only available as an option if the class from which this record was created has a mapper.

Return value:
Type Description
Object

Plain object representation of this record.

Details
Source
Record.js, line 369

unset(key, opts)

Unset the value for a given key.

Method parameters:
Name Type Argument Description
key String

Key to unset.

opts Object <optional>

Optional configuration.

Properties
Name Type Argument Default Description
silent Boolean <optional>
false

Whether to trigger change events.

Details
Source
Record.js, line 395