new Record(props, opts)
js-data's Record class.
import {Record} from 'js-data'
Name | Type | Argument | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
props |
Object |
<optional> |
The initial properties of the new Record instance. |
||||||||||
opts |
Object |
<optional> |
Configuration options. Properties
|
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.
DetailsType Since Source Object 3.0.0 Component.js, line 7 - Inherited From:
Methods
-
<static> extend(props, classProps)
-
Create a subclass of this Record.
Method parameters:Name Type Argument Default Description props
Object <optional>
{} Properties to add to the prototype of the subclass.
classProps
Object <optional>
{} Static properties to add to the subclass.
Return value:Type Description Constructor Subclass of this Record class.
Examplesimport {Record} from 'js-data' const CustomRecordClass = Record.extend({ foo () { return 'bar' } }) const customRecord = new CustomRecordClass() console.log(customRecord.foo()) // "bar"
class CustomRecordClass extends Record { foo () { return 'bar' } } const customRecord = new CustomRecordClass() console.log(customRecord.foo()) // "bar"
-
afterLoadRelations(relations, opts)
-
Lifecycle hook.
Method parameters:Name Type Description relations
Array.<String> The
relations
argument passed to Record#loadRelations.opts
Object The
opts
argument passed to Record#loadRelations. -
beforeLoadRelations(relations, opts)
-
Lifecycle hook.
Method parameters:Name Type Description relations
Array.<String> The
relations
argument passed to Record#loadRelations.opts
Object The
opts
argument passed to Record#loadRelations. -
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 Default Description equalsFn
Function <optional>
utils.deepEqual Equality function.
ignore
Array <optional>
[] Array of strings or RegExp of fields to ignore.
Return value:Type Description Object Object describing the changes to this record since it was instantiated or its Record#commit method was last called.
-
commit()
-
Make the record's current in-memory state it's only state, with any previous property values being set to current values.
-
dbg(args)
-
Log the provided values at the "debug" level.
Method parameters:Name Type Argument Description args
* <optional>
<repeatable>
Values to log.
DetailsSince 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 with the primary key of this record.
-
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.
DetailsSince Source 3.0.0 Component.js, line 98 - Inherited From:
Examplescollection.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.
-
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 Default Description equalsFn
Function <optional>
utils.deepEqual Equality function.
ignore
Array <optional>
[] Array of strings or RegExp of fields to ignore.
Return value:Type Description Boolean Return whether the record has changed since it was instantiated or since its Record#commit method was called.
-
isValid(opts)
-
Return whether the record in its current state passes validation.
Method parameters:Name Type Argument Description opts
Object <optional>
Configuration options. Passed to Mapper#validate.
Return value:Type Description Boolean Whether the record in its current state passes validation.
-
loadRelations(relations, opts)
-
Lazy load relations of this record, to be attached to the record once their loaded.
Method parameters:Name Type Argument Description relations
Array.<String> <optional>
List of relations to load.
opts
Object <optional>
Configuration options.
Return value:Type Description Promise Resolves with the record, with the loaded relations now attached.
-
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.
DetailsSince 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.
DetailsSince Source 3.0.0 Component.js, line 79 - Inherited From:
Examplescollection.off('add', handler)
record.off('change')
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.
DetailsSince Source 3.0.0 Component.js, line 49 - Inherited From:
Examplesstore.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 })
collection.on('add', (records) => { console.log(records) // [...] })
post.on('change', (record, changes) => { console.log(changes) // { changed: { title: 'Modeling your data' } } }) post.title = 'Modeling your data'
-
previous(key)
-
Return the properties with which this record was instantiated.
Method parameters:Name Type Argument Description key
String <optional>
If specified, return just the initial value of the given key.
Return value:Type Description Object The initial properties of this record.
-
revert(opts)
-
Revert changes to this record back to the properties it had when it was instantiated.
Method parameters:Name Type Argument Description opts
Object <optional>
Configuration options.
Properties
Name Type Argument Description preserve
Array.<String> <optional>
Array of strings or Regular Expressions denoting properties that should not be reverted.
-
save(opts)
-
Delegates to Mapper#create or Mapper#update.
Method parameters:Name Type Argument Description opts
Object <optional>
Configuration options. See Mapper#create and Mapper#update.
Properties
Name Type Argument Description changesOnly
Boolean <optional>
Equality function. Default uses
===
.equalsFn
Function <optional>
Passed to Record#changes when
opts.changesOnly
istrue
.ignore
Array <optional>
Passed to Record#changes when
opts.changesOnly
istrue
.Return value:Type Description Promise The result of calling Mapper#create or Mapper#update.
-
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>
Configuration options.
Properties
Name Type Argument Default Description silent
Boolean <optional>
false Whether to trigger change events.
-
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 with this record 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 and this record resides in an instance of DataStore.
Return value:Type Description Object Plain object representation of this record.
-
unset(key, opts)
-
Unset the value for a given key.
Method parameters:Name Type Argument Description key
String Key to unset.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description silent
Boolean <optional>
false Whether to trigger change events.
-
validate(opts)
-
Validate this record based on its current properties.
Method parameters:Name Type Argument Description opts
Object <optional>
Configuration options. Passed to Mapper#validate.
Return value:Type Description * Array of errors or
undefined
if no errors.