Retain Class
Retain is a browser (CJS) and node Javascript model with plugins support.
Constructor
Retain
()
Example:
var retain = require("retain");
Movies = retain();
//If you want to extend from Retain in Coffescript:
class Movies extends retain.Retain
Item Index
Attributes
Methods
all
-
[callback]
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 optionalIf suplied, it will be called when the remote records were fetched.
Returns:
Array of records.
Example:
Movies.all() // Returns the locally records
Movies.all(function(records, err)
{
// Returns the remote records
})
attrs
-
props
Define the model attributes and the attributes type.
Parameters:
-
props
ObjectObject 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]
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
NumberInstance CID or ID.
-
[callback]
Function optionalIf suplied, it will be called when the remote record was retrieved.
Returns:
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]
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 optionalIf 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
Get the value of a property.
Parameters:
-
prop
StringThe 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]
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 optionalIf suplied, it will be called when record was saved locally.
Returns:
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]
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 optionalIf 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]
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
ObjectThe properties to be setted/updated.
-
[callback]
Function optionalIf suplied, it will be called when the record was saved remotelly.
Returns:
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
Adds a plugin middleware to the Model
Parameters:
-
plugin
ObjectRetain plugin middleware
-
config
ObjectPlugin's configuration object
Example:
Movie.use(plugin_name, {
url:"/movies"
})