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.
Constructor Attributes | Constructor Name and Description |
---|---|
M.Model()
|
Field Attributes | Field Name and Description |
---|---|
The model's data provider.
|
|
Unique identifier for the model record.
|
|
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 Attributes | Method Name and Description |
---|---|
attr(type, opts)
Returns a M.ModelAttribute object to map an attribute in our record.
|
|
create(obj, dp)
Create defines a new model blueprint.
|
|
createRecord(obj)
Creates a new record of the model, means an instance of the model based on the blueprint.
|
|
del()
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)
Get attribute propName from model
|
|
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
|
|
validate()
Validates the model, means calling validate for each property.
|
- Methods borrowed from class M.Object:
- bindToCaller, destroy, extend, include
Field Detail
{Object}
dataProvider
The model's data provider.
Needs a refactoring, also in connection with storageEngine.
{Number}
id
Unique identifier for the model record.
Note: Unique doesn't mean that this id is a global unique ID, it is just unique
for records of this type of model.
{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
records
{String}
state
A constant defining the model's state. Important e.g. for syncing storage
{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.
{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 specific attributes as an 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'
{Boolean}
del()
Delete a record in storage.
- 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.
{Object|String}
get(propName)
Get attribute propName from model
- Parameters:
- {String} propName
- the name of the property whose value shall be returned
- Returns:
- {Object|String} value of property
{Boolean}
save(obj)
Create or update a record in storage if it is valid (first check this).
- Parameters:
- {Object} obj
- The param object with query 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 end is not predictable.
set(propName, val)
Set attribute propName of model with value val
- Parameters:
- {String} propName
- the name of the property whose value shall be set
- {String|Object} val
- the new value
{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).