An extension which provides a sync implementation through locally stored key value pairs, either through the HTML localStorage API or falling back onto an in-memory cache, that can be mixed into a Model or ModelList subclass.
A group of Models/ModelLists is serialized in localStorage by either its class name, or a specified 'root' that is provided.
var User = Y.Base.create('user', Y.Model, [Y.ModelSync.Local], {
root: 'user'
});
var Users = Y.Base.create('users', Y.ModelList, [Y.ModelSync.Local], {
model: User,
});
_destroy
Sync method correlating to the "delete" operation. Deletes the data from the in-memory object, and saves into localStorage if available.
_index
Sync method correlating to the "read" operation, for a Model List
Array of objects found for that root key
_save
Saves the current in-memory store into a localStorage key/value pair if localStorage is available; otherwise, does nothing.
_show
Sync method correlating to the "read" operation, for a Model
Object found for that root key and model ID
_show
Sync method correlating to the "create" operation
The new object created.
_update
Sync method correlating to the "update" operation
The updated object.
generateID
pre
Generate a random GUID for our Models. This can be overriden if you have another method of generating different IDs.
pre
String
Optional GUID prefix
sync
action
[options]
[callback]
Creates a synchronization layer with the localStorage API, if available. Otherwise, falls back to a in-memory data store.
This method is called internally by load(), save(), and destroy().
action
String
Sync action to perform. May be one of the following:
[options]
Object
optional
Sync options
[callback]
Function
optional
Called when the sync operation finishes.
err
Error | Null
If an error occurred, this parameter will contain the error. If the sync operation succeeded, err will be falsey.
[response]
Any
optional
The response from our sync. This value will be passed to the parse() method, which is expected to parse it and return an attribute hash.
_hasLocalStorage
Feature testing for localStorage
availability.
Will return falsey for browsers with localStorage
, but that don't
actually work, such as iOS Safari in private browsing mode.
_NON_ATTRS_CFG
Properties that shouldn't be turned into ad-hoc attributes when passed to a Model or ModelList constructor.
Default: ['root']
storage
Shortcut for access to localStorage.
Default: null