models.Base


Extends bindable.Object
See Also models.Collection, Application

Models represent data, and implement properties, methods, and virtuals depending on how you need to interact with that data. Just like views, models are also extendable.

Installation

npm install mojo-models --save-exact

Base(properties[, application])

base model constructor

  • properties - values to set onto the view. This could be anything.
  • application - (optional) the application. Application.main will be set if this is omitted.

data

The raw data set on the model - this is usually transformed into something the model can use via deserialize.

deserialize(data)

deserialize takes the data property on the model, and transforms the returned values as additional properties on the model.

base.serialize()

serializes data. This is an alias to toJSON

Extended API

persist

Persistence layer for models / collections. Also adds the methods load, save, and remove.

model.load(onLoad)

Defined when persist is present, and calls persist.load.

  • onLoad - onLoad callback function. Expects an (err, data) response. data is set as the data property on the model, and gets deserialized.

Note that this method can be called only once during the lifetime of the model. If you want to reload the model, you'll need to call the reload method.

model.reload(onReload)

Reloads the model. This can be called many times.

model.save(onSave)

Similar to model.load. Calls the persist.save function, and sets result to data to be deserialized on the model.

model.remove(onRemove)

Removes the model. Note that if the model is part of a models.Collection, the model will automatically be removed from the collection.

Persist events

  • willSave - emitted when the model is about to be saved
  • didSave - emitted when the model has been saved
  • willRemove - emitted when the model is about to be removed
  • didRemove - emitted when the model has been removed

See above for example.

virtuals

Virtual properties allow you to load external resources on demand. This is especially useful when you're data-binding a model property to a view layer, and only what to load what the user currently needs.

Note that virtual properties are triggered when they are data-bound.

bindings

Bindings allow you to compute properties on models.