Class: Model

Model

new Model(options)

This function creates an Model.
Parameters:
Name Type Argument Default Description
options Object <optional>
{} Object containing the properties listed below.
Properties:
Name Type Argument Default Description
name String The name of the model.
connection String The connection this model should use
url String <optional>
`/${name}` The base url of the model ('/user' for example)
api Object <optional>
{} Object the model will be extended with
schema Object <optional>
{} 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'}, {...}, {...}]

byId :Object.<Object>

Object that maps model ids to models
Type:
  • Object.<Object>
Example
< model.byId["aaedfeae53d23d23f2f31ddsd"]
> Object {name: ...}

data :Array.<Object>

Array containing all models
Type:
  • Array.<Object>
Example
< model.data[0]
> Object {name: ...}

server :Object.<function()>

Object containing functions representing requests
Type:
  • Object.<function()>
Example
model.server.login()
  .then(...);

Methods

<static> Adapter()

Adapter factory, see the frontend-communicator documentation for more information.
Example
import Model from 'frontend-model';

Model.Adapter({
  connect() {
  }
  ...
});

<static> Connection()

Connection factory, see the frontend-communicator documentation for more information.
Example
import Model from 'frontend-model';

Model.Connection({
  name: 'local-xhr',
  adapter: 'XHR',
  url: 'http://localhost:1337'
});

add(models)

Adds one or more models to the local data, if models already exist, their properties are replaced.
Parameters:
Name Type Description
models (Object|Array)} models to be added
Example
model.add({});
model.add([{}]);
model.add([{}], {});

clone(model) → {Object}

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)
Parameters:
Name Type Description
model object model in question
Returns:
Type
boolean
Example
< model.isNew({
  name: 'bob'
});
> false

< model.isNew({
 id: 3
});
> true

listenTo(model, event, callback) → {Object}

Listens to changes in one, or all models.
Parameters:
Name Type Argument Description
model Object <optional>
Model to listen to
event Object <optional>
Event to listen to
callback function Function to call when the event has been triggered for this model.
Returns:
Listener object, this is used to stop listening, by passing it into stopListeningTo, or by calling the stop() method provided on it
Type
Object
Example
user.listenTo(model, function (changedModel) {...});
user.listenTo(function (changedModel) {...});

off(event, callback) → {*}

Stops removes one or all listeners from an event
Parameters:
Name Type Argument Description
event String The event that has to be stopped listening to
callback function <optional>
The callback that should be removed
Returns:
Type
*

on(event, callback) → {*}

Listens to an event and triggers the callback when it occurs.
Parameters:
Name Type Description
event String The event to listen for
callback function The function to trigger when the event has occurred
Returns:
Type
*

once(event, callback) → {*}

Listens to an event and triggers the callback when it occurs. After this happens the listener is removed.
Parameters:
Name Type Description
event String The event to listen for
callback function The function to trigger when the event has occurred
Returns:
Type
*

remove(models)

Removes one or more models from the local data, to remove a model from the server see Model#destroy.
Parameters:
Name Type Argument Description
models Object | Array.<Object> <repeatable>
models to be removed
Example
// etc.
model.remove({});
model.remove([{}]);
model.remove([{}], {});

reset() → {Promise}

Resets the data of the model to the state of the server. Gets rid of any changes that haven't been saved yet.
Returns:
Type
Promise
Example
model.reset()
  .then(...)

save(model) → {Promise}

Saves a model to the server, if model is provided, all to be saved models will be saved to the server (all locally created and changed models)
Parameters:
Name Type Argument Description
model Object <optional>
The model to save, if none specified, all models that are to be saved will be saved
Returns:
Type
Promise
Example
// to save all newly created or updated models
model.save()
  .then(...);

// save one model
model.save({
    name: 'bob'
  })
  .then(...);

set(model, properties)

See Model#add
Parameters:
Name Type Description
model Object model that is to be set
properties Object new properties that have to be set
Example
model.set(model.data[0], {firstName: 'bob});

sync() → {Promise}

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
Returns:
Type
Promise
Example
model.sync()
  .then(...);

trigger(event, data) → {*}

Triggers an event with data
Parameters:
Name Type Description
event String Event to trigger
data * Data to trigger with the event
Returns:
Type
*
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.4.0 on 2015-12-03T17:37:53+01:00