Class M.DataProviderLocalStorage
Extends
M.DataProvider.
Encapsulates access to LocalStorage (in-browser key value store).
LocalStorage is an in-browser key-value store to persist data.
This data provider persists model records as JSON strings with their name and id as key.
When fetching these strings from storage, their automatically converted in their corresponding model records.
Operates synchronous.
Defined in: local_storage.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Field Attributes | Field Name and Description |
---|---|
The type of this object.
|
- Fields borrowed from class M.DataProvider:
- isAsync
Method Attributes | Method Name and Description |
---|---|
allKeys(obj)
Returns all keys for model defined by modelName.
|
|
buildRecord(key, obj)
Fetches a record from LocalStorage and checks whether automatic parsing by JSON.parse set the elements right.
|
|
del(obj)
deletes a model from the local storage
key defines which one to delete
e.g.
|
|
find(The)
Finds all models of type defined by modelName that match a key or a simple query.
|
|
findAll(obj)
Returns all models defined by modelName.
|
|
findByKey(The)
Finds a record identified by the key.
|
|
save(that)
Saves a model record to the local storage
The key is the model record's name combined with id, value is stringified object
e.g.
|
- Methods borrowed from class M.DataProvider:
- check
- Methods borrowed from class M.Object:
- bindToCaller, create, destroy, extend, get, include, set
Field Detail
{String}
type
The type of this object.
Method Detail
{Object}
allKeys(obj)
Returns all keys for model defined by modelName.
- Parameters:
- {Object} obj
- The param obj, includes model
- Returns:
- {Object} keys All keys for model records in LocalStorage for a certain model identified by the model's name.
{Object}
buildRecord(key, obj)
Fetches a record from LocalStorage and checks whether automatic parsing by JSON.parse set the elements right.
Means: check whether resulting object's properties have the data type define by their model attribute object.
E.g. String containing a date is automatically transfered into a M.Date object when the model attribute has the data type
'Date' set for this property.
- Parameters:
- {String} key
- The key to fetch the element from LocalStorage
- {Object} obj
- The param object, includes model
- Returns:
- {Object} record The record object. Includes all model record properties with correctly set data types.
{Boolean}
del(obj)
deletes a model from the local storage
key defines which one to delete
e.g. key: 'Note_123'
- Parameters:
- {Object} obj
- The param obj, includes model
- Returns:
- {Boolean} Boolean indicating whether save was successful (YES|true) or not (NO|false).
{Object|Boolean}
find(The)
Finds all models of type defined by modelName that match a key or a simple query.
A simple query example: 'price < 2.21'
Right now, no AND or OR joins possible, just one query constraint.
If no query is passed, all models are returned by calling findAll()
- Parameters:
- {Object} The
- param object containing e.g. the query or the key.
- Throws:
- Exception when query tries to compare two different data types
- Returns:
- {Object|Boolean} Returns an object if find is done with a key, an array of objects when a query is given or no parameter passed.
{Object}
findAll(obj)
Returns all models defined by modelName.
Models are saved with key: Modelname_ID, e.g. Note_123
- Parameters:
- {Object} obj
- The param obj, includes model
- Returns:
- {Object} The array of fetched objects/model records. If no records the array is empty.
{Object|Boolean}
findByKey(The)
Finds a record identified by the key.
- Parameters:
- {Object} The
- param object containing e.g. the query or the key.
- Returns:
- {Object|Boolean} Returns an object identified by key, correctly built as a model record by calling or a boolean (NO|false) if no key is given or the key does not exist in LocalStorage. parameter passed.
{Boolean}
save(that)
Saves a model record to the local storage
The key is the model record's name combined with id, value is stringified object
e.g.
Note_123 => '{ text: 'buy some food' }'
- Parameters:
- {Object} that
- (is a model).
- Returns:
- {Boolean} Boolean indicating whether save was successful (YES|true) or not (NO|false).