API Docs for:
Show:

Retain Class

Defined in: lib/index.js:12

Retain is a browser (CJS) and node Javascript model with plugins support.

Constructor

Retain

()

Defined in lib/index.js:12

Example:

 var retain = require("retain");

 Movies = retain();

 //If you want to extend from Retain in Coffescript:

 class Movies extends retain.Retain

Item Index

Methods

Attributes

Methods

all

(
  • [callback]
)
Array static

Defined in lib/index.js:273

Get all the model instances

If a callback is suplied, and there is at least one plugin attached to the model, fetch the records remotelly.

Parameters:

  • [callback] Function optional

    If suplied, it will be called when the remote records were fetched.

Returns:

Array:

Array of records.

Example:

   Movies.all() // Returns the locally records

   Movies.all(function(records, err)
   {
     // Returns the remote records
   })

attrs

(
  • props
)
static

Defined in lib/index.js:104

Define the model attributes and the attributes type.

Parameters:

  • props Object

    Object containing the attributes and types as key:values.

Example:

   Movie.attrs({
     name:String,
     watched:Boolean,
     duration: Number,
     categories: Array,
     info: Object,
     year: Date
   })

find

(
  • id
  • [callback]
)
Object static

Defined in lib/index.js:220

Finds a model instance based on the CID or ID.

If a callback is suplied, and there is at least one plugin attached to the model, finds the record remotelly.

Parameters:

  • id Number

    Instance CID or ID.

  • [callback] Function optional

    If suplied, it will be called when the remote record was retrieved.

Returns:

Object:

Record found.

Example:

   var eyesWideShut = Movies.new();

   Movies.find(0) // Returns a model instance (eyesWideShut)

   // Searchs remotelly for a recod with the ID of 2
   Movies.find(2, function(movie, err)
   {

   });

find

(
  • [callback]
)

Defined in lib/index.js:298

Removes/deletes the record locally.

If a callback is suplied, and there is at least one plugin attached to the model, removes the record remotelly.

Parameters:

  • [callback] Function optional

    If suplied, it will be called when the record was removed/deleted remotelly.

Example:

   var eyesWideShut = Movies.new(function()
   {
     // Creates a new record remotelly
   });

   eyesWideShut.remove() // Removes/deletes the record locally

   eyesWideShut.remove(function(movie, err)
   {
     // Removes/deletes the record remotelly.
   })

get

(
  • prop
)

Defined in lib/index.js:201

Get the value of a property.

Parameters:

  • prop String

    The property to be retrieved.

Returns:

The property value.

Example:

   factotum.set({name:"Factotum", watched: false}) // Updates the record locally

   var name = factotum.get("name") //Returns 'Factotum'

new

(
  • [callback]
)
Object static

Defined in lib/index.js:130

Creates a new model instance locally. If a callback is suplied, and there is at least one plugin attached to the model, creates the record remotelly.

Parameters:

  • [callback] Function optional

    If suplied, it will be called when record was saved locally.

Returns:

Object:

Model instance.

Example:

   var godfather = Movies.new() //Creates the record locally
   var godfatherRemote = Movies.new(function(movie, err)
   {
     //Creates the record locally, and remotelly (if there is any plugin attached)
   })

save

(
  • [callback]
)

Defined in lib/index.js:339

Sync the local record with the remote storages.

If a callback is suplied, and there is at least one plugin attached to the model, syncs the record remotelly.

Parameters:

  • [callback] Function optional

    If suplied, it will be called when the record was synchronized remotelly.

Example:

   var graveOfTheFireflies = Movie.new();

   graveOfTheFireflies.set({name:"Grave of the Fireflies"});

   graveOfTheFireflies.save(function()
   {
     done();
   });

set

(
  • props
  • [callback]
)
Object

Defined in lib/index.js:162

Sets the model instance properties locally.

If a callback is suplied, and there is at least one plugin attached to the model, updates the record remotelly.

Parameters:

  • props Object

    The properties to be setted/updated.

  • [callback] Function optional

    If suplied, it will be called when the record was saved remotelly.

Returns:

Object:

Model instance updated.

Example:

   var factotum = Movies.new(function(movie, err)
   {
     //Creates the record locally and remotelly
   })

   factotum.set({name:"Factotum", watched: false}) // Updates the record locally
   factotum.set({name:"Factotum", watched: false}, function()
   {
     // Updates the record remotelly.
   });

use

(
  • plugin
  • config
)
static

Defined in lib/index.js:77

Adds a plugin middleware to the Model

Parameters:

  • plugin Object

    Retain plugin middleware

  • config Object

    Plugin's configuration object

Example:

   Movie.use(plugin_name, {
     url:"/movies"
   })

Attributes

id_prop

Defined in lib/index.js:66

Changes the name of the 'ID' property.

Example:

   Movies.id_prop = "_id"; // Useful when working with data coming from MongoDB (it uses '_id')