Class Index | File Index

Classes


Class M.Model


Extends M.Object.
M.Model is the prototype for every model and for every model record (a model itself is the blueprint for a model record). Models hold the business data of an application respectively the application's state. It's usually the part of an application that is persisted to storage. M.Model acts as the gatekeeper to storage. It uses data provider for persistence and validators to validate its records.
Defined in: model.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
Field Summary
Field Attributes Field Name and Description
 
The model's data provider.
 
Unique identifier for the model record.
 
List containing all models in application
 
The name of the model.
 
The model's record defines the properties that are semantically bound to this model: e.g.
 
Manages records of this model
 
A constant defining the model's state.
 
 
The type of this object.
 
determines whether model shall be validated before saving to storage or not.
Method Summary
Method Attributes Method Name and Description
 
attr(type, opts)
Returns a M.ModelAttribute object to map an attribute in our record.
 
complete(obj)
completes the model record by loading all referenced entities.
 
create(obj, dp)
Create defines a new model blueprint.
 
Creates a new record of the model, means an instance of the model based on the blueprint.
 
deepFind(records, callback)
 
del(obj)
Delete a record in storage.
 
find(obj)
Calls the corresponding find() of the data provider to fetch data based on the passed query or key.
 
get(propName, obj)
Get attribute propName from model, if async provider is used, get on references does not return the property value but a boolean indicating the load status.
 
 
hasMany(colName, refEntity, invRel)
Defines a to-many-relationship
 
hasOne(refName, refEntity)
Defines a to-one-relationship.
 
Returns the records array of the model's record manager.
 
save(obj)
Create or update a record in storage if it is valid (first check this).
 
set(propName, val)
Set attribute propName of model with value val, sets' property to isUpdated (=> will be included in UPDATE call) and sets a new timestamp to _updatedAt.
 
setReference(result, that, prop, callback)
 
Validates the model, means calling validate for each property.
Methods borrowed from class M.Object:
bindToCaller, destroy, extend, include
Class Detail
M.Model()
Field Detail
{Object} dataProvider
The model's data provider. A data provider persists the model to a certain storage.

{String} m_id
Unique identifier for the model record. It's as unique as it needs to be: four digits, each digits can be one of 32 chars

{Object|Array} modelList
List containing all models in application

{String} name
The name of the model.

{Object record} record
The model's record defines the properties that are semantically bound to this model: e.g. a person's record is (in simplest case): firstname, lastname, age.

{Object} recordManager
Manages records of this model

{String} state
A constant defining the model's state. Important e.g. for syncing storage

{String} state_remote

{String} type
The type of this object.

{Boolean} usesValidation
determines whether model shall be validated before saving to storage or not.
Method Detail
{Object} attr(type, opts)
Returns a M.ModelAttribute object to map an attribute in our record.
Parameters:
{String} type
type of the attribute
{Object} opts
options for the attribute, like required flag and validators array
Returns:
{Object} An M.ModelAttribute object configured with the type and options passed to the function.

complete(obj)
completes the model record by loading all referenced entities.
Parameters:
{Function | Object} obj
The param object with query, cascade flag and callbacks.

{Object} create(obj, dp)
Create defines a new model blueprint. It is passed an object with the model's attributes and the model's business logic and after it the type of data provider to use.
Parameters:
{Object} obj
An object defining the model's
{Object} dp
The data provider to use, e. g. M.LocalStorageProvider
Returns:
{Object} The model blueprint: acts as blueprint to all records created with @link M.Model#createRecord

{Object} createRecord(obj)
Creates a new record of the model, means an instance of the model based on the blueprint. You pass the object's specific attributes to it as an object.
Parameters:
{Object} obj
The properties object, e.g. {firstname: 'peter', lastname ='fox'}
Returns:
{Object} The model record with the passed properties set. State depends on newly creation or fetch from storage: if from storage then state is M.STATE_NEW or 'state_new', if fetched from database then it is M.STATE_VALID or 'state_valid'

deepFind(records, callback)
Parameters:
records
callback

{Boolean} del(obj)
Delete a record in storage.
Parameters:
obj
Returns:
{Boolean} Indicating whether deletion was successful or not (only with synchronous data providers, e.g. LocalStorage). When asynchronous data providers are used, e.g. WebSQL provider the real result comes asynchronous and here just the result of the del() function call of the @link M.WebSqlProvider is used.

{Boolean|Object} find(obj)
Calls the corresponding find() of the data provider to fetch data based on the passed query or key.
Parameters:
{Object} obj
The param object with query or key and callbacks.
Returns:
{Boolean|Object} Depends on data provider used. When WebSQL used, a boolean is returned, the find result is returned asynchronously, because the call itself is asynchronous. If LocalStorage is used, the result of the query is returned.

{Boolean|Object|String} get(propName, obj)
Get attribute propName from model, if async provider is used, get on references does not return the property value but a boolean indicating the load status. get must then be called again in onSuccess callback to retrieve the value
Parameters:
{String} propName
the name of the property whose value shall be returned
{Object} obj
optional parameter containing the load force flag and callbacks, e.g.: { force: YES, onSuccess: function() { console.log('yeah'); } }
Returns:
{Boolean|Object|String} value of property or boolean indicating the load status

getUniqueId()

hasMany(colName, refEntity, invRel)
Defines a to-many-relationship
Parameters:
colName
refEntity
invRel

hasOne(refName, refEntity)
Defines a to-one-relationship.
Parameters:
refName
refEntity

{Object|Array} records()
Returns the records array of the model's record manager.
Returns:
{Object|Array} The records array of record manager.

{Boolean} save(obj)
Create or update a record in storage if it is valid (first check this). If param obj includes cascade:YES then save is cascadaded through all references recursively.
Parameters:
{Object} obj
The param object with query, cascade flag and callbacks.
Returns:
{Boolean} The result of the data provider function call. Is a boolean. With LocalStorage used, it indicates if the save operation was successful. When WebSQL is used, the result of the save operation returns asynchronously. The result then is just the standard result returned by the web sql provider's save method which does not necessarily indicate whether the operation was successful, because the operation is asynchronous, means the operation's result is not predictable.

set(propName, val)
Set attribute propName of model with value val, sets' property to isUpdated (=> will be included in UPDATE call) and sets a new timestamp to _updatedAt. Will not do anything, if newVal is the same as the current prop value.
Parameters:
{String} propName
the name of the property whose value shall be set
{String|Object} val
the new value

setReference(result, that, prop, callback)
Parameters:
result
that
prop
callback

{Boolean} validate()
Validates the model, means calling validate for each property.
Returns:
{Boolean} Indicating whether this record is valid (YES|true) or not (NO|false).

Documentation generated by JsDoc Toolkit 2.4.0 on Wed Jan 25 2012 20:41:10 GMT+0100 (MEZ)