Object describing the properties of a Model, NOT IMPLEMENTED YET
defaults
Object
<optional>
{}
Object with default properties of the model, schema will deprecate this property
event
String
<optional>
The models name
Server event for the model, 'user' for example
requests
String
<optional>
{}
Requests for this model, other than the restful findAll(), findById(id), create(model), update(id, model), destroy(id) methods, which are created automatically
idAttribute
String
<optional>
'id'
The attribute on which the id of the model resides.
createdOnAttribute
String
<optional>
'createdAt'
The attribute on which the created on property of the model resides.
updatedOnAttribute
String
<optional>
'updatedAt'
The attribute on which the updated on property of the model resides.
Author:
Rik Hoffbauer
To Do:
handle connection events
implement schema, deprecate defaults
tbd: do we want to allow for transformer functions to be defined that are executed before and after a request to the server is made
allow idAttribute, createdAtAttribute and updateAtAttribute to be implemented as functions
Example
import Model from 'frontend-model';
const model = Model({
name: 'user',
url: '/user',
connection: 'local-xhr',
requests: {
login: {
route: '/user/login',
method: 'get'
}
},
event: 'user',
idAttribute: 'id'
});
Members
<static> byId :Object
All data for all models by id
Type:
Object
Example
import Model from 'frontend-model';
Model.models.byId; // {1: {name: 'bob'}, 2: {...}, 3: {...}}
<static> communicator :Object
Communicator used by the Model to communicate with the server, see the frontend-communicator documentation for more information.
Type:
Object
Example
import Model from 'frontend-model';
Model.communicator.connect('local-xhr')
.then(...);
<static> defaults :Object
The default values for a model, these may be changed.
Type:
Object
Properties:
Name
Type
Argument
Default
Description
name
String
<optional>
''
Shouldn't be set to anything, default name of a model should be an empty string
connection
String
<optional>
''
Connection might be useful to set, so you don't have to provide it to every model
requests
String
<optional>
{}
You can create requests that become available for every model
api
String
<optional>
{}
Provide a default api for all models
schema
String
<optional>
{}
Provide a default schema for all models
defaults
String
<optional>
{}
Set defaults for all models, probably not useful
idAttribute
String
<optional>
'id'
Set the idAttribute of every object, might be useful
updatedOnAttribute
String
<optional>
'updatedAt
Set the updatedAt of every object, might be useful
createdOnAttribute
String
<optional>
'createdAt'
Set the createdAt of every object, might be useful
Example
import Model from 'frontend-model';
Model.defaults.connection = 'local-xhr';
<static> models :Object
All data for all models
Type:
Object
Example
import Model from 'frontend-model';
Model.models.users; // [{name: 'bob'}, {...}, {...}]
Clones a model, by cloning the object and removing its id, createOn and updatedOn properties. The attributes of these three properties can be configured using the idAttribute, createdOnAttribute and updatedOnAttribute.
Parameters:
Name
Type
Description
model
Object
Model that is to be cloned
Returns:
Type
Object
Example
const clonedModel = model.clone({
name: 'bob'
});
// to save to the server:
model.sync(); // syncs everything, most expensive
// or
model.save(); // saves everything, significantly less expensive
// or
model.save(clonedModel); // saves just this model, least expensive
create(attributes) → {Object}
Creates a a new model, and adds it to the local data, when sync(), save() or save(createdModel) is called it will be saved to the server.
Parameters:
Name
Type
Description
attributes
Object
Properties of the model to create
Returns:
Type
Object
Example
const createdModel = model.create({
name: 'bob'
});
// to save to the server:
model.sync(); // syncs everything, most expensive
// or
model.save(); // saves everything, significantly less expensive
// or
model.save(createdModel); // saves just this model, least expensive
destroy(model) → {Promise}
Destroy a model on the server, to remove a model locally see Model#remove
Parameters:
Name
Type
Argument
Description
model
Object
|
String
|
Number
<optional>
Model or model id
Returns:
Type
Promise
Example
// to destroy all newly created or updated models
model.destroy()
.then(...);
// destroy one model by object
model.save({
id: 3
})
.then(...);
// destroy one model by id
model.destroy(3)
.then(...);
fetch(model) → {Promise}
Fetches a model from the server
Parameters:
Name
Type
Description
model
Object
|
String
|
Number
Model or model id
Returns:
Type
Promise
Example
// fetch all models
model.fetch()
.then(...);
// fetch one model by object
model.fetch({
id: 3,
...
})
.then(...);
// fetch one model by id
model.fetch(3)
.then(...);
id(model) → {*}
Gets the id for a model
Parameters:
Name
Type
Description
model
Object
|
String
|
Number
Model or id
Returns:
Type
*
Example
model.id({...});
isNew(model) → {boolean}
Determines whether a model is new (doesn't exist on the server)
Syncs the local data with server data, first destroys and saves models from and to the server that have been removed / changed / created locally.
After removing and saving local models to the server, the current data is fetched from the server