Class: Model

Rekord. Model

new Rekord.Model(db)

An instance

Name Type Description
db Rekord.Database

The database instance used in model instances.

Extends

Methods

staticRekord.Model.all(){Rekord.ModelCollection}

Returns the reference to the collection which contains all saved models.

var Task = Rekord({
  fields: ['name', 'done']
});
var t0 = Task.create({name: 't0', done: true}); // saves
var t1 = new Task({name: 't1'});
Task.all(); // [t0]
Returns:
Type Description
Rekord.ModelCollection - The reference to the collection of models.

Returns an instance of a model or model collection with remote data (from the server). If the model(s) exist locally then the values passed in will overwrite the current values of the models. This is typically used to bootstrap data from the server in your webpage.

var User = Rekord({
  fields: ['name', 'email']
});
var currentUser = User.boot({
  id: 1234,
  name: 'Administrator',
  email: 'rekordjs@gmail.com'
});
var friends = User.boot([
  { id: 'c1', name: 'Cat 1', email: 'cat1@gmail.com' },
  { id: 'c2', name: 'Cat 2', email: 'cat2@gmail.com' }
]);
Type Description
Array.<modelInput> | Object
Returns:
Type Description
Rekord.ModelCollection | Rekord.Model - The collection or model bootstrapped.

staticRekord.Model.collect(models){Rekord.ModelCollection}

Creates a collection of models.

var Task = Rekord({
  fields: ['name']
});
var t0 = Task.create({id: 34, name: 't0'});
var t1 = new Task({name: 't1'});
var t2 = {name: 't2'};

var c = Task.collect( 34, t1, t2 ); // or Task.collect( [34, t1, t2] )
c; // [t0, t1, t2]
Name Type Description
models Array.<modelInput> | modelInput

The array of models to to return as a collection.

Returns:
Type Description
Rekord.ModelCollection - The collection created.

staticRekord.Model.create(props){Rekord.Model}

Creates a model instance, saves it, and returns it.

var Task = Rekord({
 fields: ['name'],
 defaults: {
   name: 'New Task'
 }
});
var t0 = Task.create({id: 34, name: 't0'});
var t1 = Task.create({name: 't1'}); // id generated with uuid
var t2 = Task.create(); // name populated with default 'New Task'
Name Type Description
props Object optional

The initial values for the new model - if any.

Returns:
Type Description
Rekord.Model - The saved model instance.

staticRekord.Model.fetch(input, callback, context){Rekord.Model}

Gets the local model matching the given input (or creates one) and loads it from the remote source (Rekord.rest). If callback is specified then it is invoked with the instance once it's loaded.

var Task = Rekord({
 fields: ['name']
});
var t0 = Task.fetch( 34, function(task) {
  task; // {id: 34 name: 'Remotely Loaded'}
});
t0; // {id: 34} until remotely loaded
Name Type Description
input modelInput

The model input used to determine the key and load the model.

callback function optional

The function to invoke passing the reference of the model once it's successfully remotely loaded.

context Object optional

The context (this) for the callback.

Returns:
Type Description
Rekord.Model - The model instance.

staticRekord.Model.fetchAll(callback, context){Rekord.ModelCollection}

Returns the collection of all local models and tries to reload them (and any additional models returned) from a remote source (Rekord.rest). If callback is specified then it is invoked with the collections all models once it's loaded.

var Task = Rekord({
 fields: ['name']
});
var tasks0 = Task.fetchAll( function(tasks1) {
  tasks0 // tasks1
});
Name Type Description
callback function optional

The function to invoke passing the reference of the model collection when it's successfully remotely loaded.

context Object optional

The context (this) for the callback.

Returns:
Type Description
Rekord.ModelCollection - The collection of all models of this type.

staticRekord.Model.get(input, callback, context){Rekord.Model}

Returns the model instance identified with the given input. This includes saved and unsaved models. If a callback is given the model will be passed to the function. The callback method is useful for waiting for Rekord to finish initializing (which includes loading models from local storage followed by remote storage if configured) and returning a model instance. If Rekord has finished initializing and the model doesn't exist locally then it is fetched from the remoute source using Rekord.rest.

var Task = Rekord({
 fields: ['name']
});
var t0 = Task.get( 34 ); // only looks at models currently loaded
var t1 = Task.get( 23, function(model) {
  model; // local or remotely loaded if it didn't exist locally - could be null if it doesn't exist at all
})
Name Type Description
input modelInput

The model input used to determine the key and load the model.

callback function optional

The function to invoke passing the reference of the model when it's successfully found.

context Object optional

The context (this) for the callback.

Returns:
Type Description
Rekord.Model - The model instance if callback is not given - or undefined if the input doesn't resolve to a model or callback is given.

staticRekord.Model.grab(input, callback, context){Rekord.Model}

Gets the model instance identified with the given input and passes it to the callback function. If Rekord is not finished initializing this function will wait until it is and check for the model. If it still doesn't exist locally it is loaded from a remote source using Rekord.rest. If the model doesn't exist at all a null value will be returned to the function.

var Task = Rekord({
 fields: ['name']
});
var t1 = Task.grab( 23, function(model) {
  model; // local or remotely loaded if it didn't exist locally - could be null if it doesn't exist at all
})
Name Type Description
input modelInput

The model input used to determine the key and load the model.

callback function

The function to invoke passing the reference of the model when it's successfully found.

context Object optional

The context (this) for the callback.

Returns:
Type Description
Rekord.Model - The model instance of it exists locally at the moment, or undefined if the model hasn't been loaded yet.

staticRekord.Model.grabAll(callback, context){Rekord.Model}

Gets all model instances currently loaded, locally loaded, or remotely loaded and passes it to the callback function.

var Task = Rekord({
 fields: ['name']
});
var tasks = Task.grabAll( function(models) {
  models; // local or remotely loaded if it didn't exist locally.
})
Name Type Description
callback function

The function to invoke passing the reference of the model collection when it's loaded.

context Object optional

The context (this) for the callback.

Returns:
Type Description
Rekord.Model - The model collection of it exists locally at the moment, or undefined if models haven't been loaded yet.

staticRekord.Model.persist(input){Rekord.Model}

Persists model values, creating a model instance if none exists already (determined by the key derived from the input).

var ListItem = Rekord({
 key: ['list_id', 'iten_id'],
 fields: ['quantity'],
 belongsTo: {
   list: { model: 'list' },
   item: { model: 'item' }
 }
});

var listItem = ListItem.persist({ // creates relationship if it doesn't exist already - updates existing
 list: someList,
 item: someItem,
 quantity: 23
});
Name Type Description
input Object optional

The values to persist in the model instance found or created.

Returns:
Type Description
Rekord.Model - The saved model instance or undefined if the model database has not finished loading.

staticRekord.Model.persist(input){Rekord.Model}

Finds or creates a model instance based on the given values. The key for the model must be derivable from the given values - or this function will always create a new model instance.

var ListItem = Rekord({
 key: ['list_id', 'iten_id'],
 fields: ['quantity'],
 belongsTo: {
   list: { model: 'list' },
   item: { model: 'item' }
 }
});

var listItem = ListItem.findOrCreate({
 list: someList,
 item: someItem,
 quantity: 23
});
// do stuff with listItem
Name Type Description
input Object optional

The values to set in the model instance found or created.

Returns:
Type Description
Rekord.Model - The saved model instance or undefined if the model database has not finished loading.

staticRekord.Model.ready(callback, context, persistent)

Invokes a function when Rekord has loaded. It's considered loaded when it's loaded locally, remotely, or neither (depending on the options passed to the database). The callback can also be invoked persistently on any load event - which includes Rekord.Database#refresh.

var Task = Rekord({
 fields: ['name']
});
Task.ready( function(db) {
 // Tasks have been loaded, lets do something about it!
});
Name Type Default Description
callback function

The function to invoke passing the reference of the database when it's loaded.

context Object optional

The context (this) for the callback.

persistent Boolean false optional

Whether the callback function should be invoked multiple times. Depending on the state of initializing, the callback can be invoked when models are loaded locally (if the cache is not equal to None), models are loaded remotely (if load is Rekord.Load.All), and every time Rekord.Database#refresh is called manually OR if autoRefresh is specified as true and the application changes from offline to online.

staticRekord.Model.refresh(callback, context)

Refreshs the model database from the remote source by calling Rekord.Database#refresh. A callback can be passed to be invoked when the model database has refreshed (or failed to refresh) where all models that have been loaded will be passed as the first argument.

var Task = Rekord({
 fields: ['name']
});
Task.refresh( function(models) {
 models; // The collection of models loaded remotely (or current models if it failed to load them remotely.
});
Name Type Description
callback function

The function to invoke passing the reference model collection.

context Object optional

The context (this) for the callback.

staticRekord.Model.search(url, options, props, run){Rekord.Search}

Creates a new search for model instances. A search is an object with properties that are passed to a configurable Rekord.rest function which expect an array of models to be returned from the remote call that match the search parameters.

var Task = Rekord({
 fields: ['name', 'done']
});
var search = Task.search('/api/task/search');
search.name = 'like this';
search.done = true;
search.anyProperty = [1, 3, 4];
var promise = search.$run();
promise.success( function(search) {
  search.$results; // collection of returned results
});
Name Type Default Description
url String

A URL to send the search data to.

options searchOptions optional

Options for the search.

props Object optional

Initial set of properties on the search.

run Boolean false optional

Whether or not to run the search immediately.

Returns:
Type Description
Rekord.Search - A new search for models.

staticRekord.Model.searchPaged(url, options, props, run){Rekord.SearchPaged}

Creates a new search with pagination for model instances. A paginated search is an object with properties that are passed to a configurable Rekord.rest function which expect an array of models to be returned as well as paging information from the remote call. Special properties are passed to the server (page_index, page_size) which dictate which chunk of data should be returned. A special total property is expected to be returned with results which tells the search how many records would've been returned without the pagination.

var Task = Rekord({
 fields: ['name', 'done']
});
var search = Task.searchPaged('/api/task/searchPaged');
search.name = 'like this';
search.done = true;
search.anyProperty = [1, 3, 4];
var promise = search.$run();
promise.success( function(search) {
  search.$results; // collection of returned results
  search.total; // number of results that would've been returned without pagination
  search.page_index; // the zero-based page index
  search.page_size; // the number of results to be returned
});
search.$next(); // increase page_index, get the next page
Name Type Default Description
url String

A URL to send the search data to.

options searchPageOptions optional

Options for the search.

props Object optional

Initial set of properties on the search.

run Boolean false optional

Whether or not to run the search immediately.

Returns:
Type Description
Rekord.SearchPaged - A new paginated search for models.

inherited $on(events, callback, context){function}

Listens for every occurrence of the given events and invokes the callback each time any of them are triggered.

Name Type Description
events String | Array

The event or events to listen to.

callback function

The function to invoke when any of the events are invoked.

context Object optional

The value of this when the callback is invoked. If not specified, the reference of the object this function exists on will be this.

Returns:
Type Description
function - A function to invoke to stop listening to all of the events given.

inherited $once(events, callback, context){function}

Listens for the first of the given events to be triggered and invokes the callback once.

Name Type Description
events String | Array

The event or events to listen to.

callback function

The function to invoke when any of the events are invoked.

context Object optional

The value of this when the callback is invoked. If not specified, the reference of the object this function exists on will be this.

Returns:
Type Description
function - A function to invoke to stop listening to all of the events given.