Sprinkle Supersonic Data models with magical Composer Module sauce
# Replace this:
Car = supersonic.data.model 'car'
# With this:
magical = require 'ag-magic-model'
Car = magical 'car'
# Bam, your model is now magical!
Once sprinkled on a supersonic.data.model
class, ag-magic-model
adds the magical
property on the class. It can be used to access features enabled when integrating with Composer.
Access the plain name of the resource this model is backed by.
Access the complete definition object from Composer used by this model.
Access the formatted label of a field.
Access route strings usable as supersonic.module
navigation functionality parameters, eg. supersonic.module.modal.show
.
Model.magical.routes.new
: route for creating a new record of this typeModel.magical.routes.show
: route for showing an existing record of this type; requiresrecord-id
The plural title for a collection of records of this type.
The singular title for records of this type.
The formatted title for a specific record of this type.
Access the formatter function for a field. Call the function with a field value to get it back as formatted.
Formatting takes into account the field's type, and possible other configuration options defined in Composer. For instance, a field with they display type date
will be formatted as YYYY-MM-DD
by default, but this can be overridden by configuring the data field in Composer.
NOTE: Records and collections fetched via Model.magical.relations.join
are read only. It is not appropriate to save()
such records: they have gotten their fields tampered with, and the underlying resource does not know how to map the joined contents back to their normalized representations.
Get a followable of all
records such that they will get the contents of enumerated fields
listed joined in asynchronously. For example:
Model.magical.relations
.join('foo', 'bar')
.all()
.changes
.onValue (record) ->
console.log {
foo: record.foo.title
bar: record.bar.title
}
For a relation field, a field foo
containing the id 123
will be replaced with:
id: 123
title: <the title for 123 rendered as a string>
record: <the Model instance for 123>
For a multirelation field foos
with 123
and 456
:
[
{
id: 123
title: <the title for 123 rendered as a string>
record: <the Model instance for 123>
}
{
id: 456
title: <the title for 456 rendered as a string>
record: <the Model instance for 456>
}
]
Because the join is done asynchronously, the contents will start as having a placeholder for the title
and nothing for record
.
Get a followable of one
record by id such that it will get the contents of enumerated fields
listed joined in asynchronously. Works as join().all()
, above.
Get a followable of many
records loaded by ids
as a collection of related records that will get their contents asynchronously.
Model.magical.relations
.related('foo')
.many([123, 456])
.changes
.onValue (collection) ->
console.log collection
The collection's signature is the same as with Model.magical.relations.join
.
Get a followable of one
record loaded by ids
such that it will get its contents asynchronously.
The record's signature is the same as with records from Model.magical.relations.join
.