Show:
Module: app

Available since 3.13.0

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,
                            });

Methods

_destroy

() protected

Sync method correlating to the "delete" operation. Deletes the data from the in-memory object, and saves into localStorage if available.

_index

() Object protected

Sync method correlating to the "read" operation, for a Model List

Returns:

Object:

Array of objects found for that root key

_save

() protected

Saves the current in-memory store into a localStorage key/value pair if localStorage is available; otherwise, does nothing.

_show

() Object protected

Sync method correlating to the "read" operation, for a Model

Returns:

Object:

Object found for that root key and model ID

_show

() Object protected

Sync method correlating to the "create" operation

Returns:

Object:

The new object created.

_update

() Object protected

Sync method correlating to the "update" operation

Returns:

Object:

The updated object.

generateID

(
  • pre
)
protected

Generate a random GUID for our Models. This can be overriden if you have another method of generating different IDs.

Parameters:

  • 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().

Parameters:

  • action String

    Sync action to perform. May be one of the following:

    • create: Store a newly-created model for the first time.
    • read : Load an existing model.
    • update: Update an existing model.
    • delete: Delete an existing model.
  • [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.

Properties

_data

Object private

Object of key/value pairs to fall back on when localStorage is not available.

_hasLocalStorage

Boolean private

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

Array protected static

Properties that shouldn't be turned into ad-hoc attributes when passed to a Model or ModelList constructor.

Default: ['root']

_store

Array private

Cache to quickly access a specific object with a given ID.

root

String

Root used as the key inside of localStorage and/or the in-memory store.

Default: ""

storage

Storage

Shortcut for access to localStorage.

Default: null