Adapter: Memory

AeroGear.DataManager.adapters. Memory

new Memory(storeName, settings) → {Object}

Status: Stable
The Memory adapter is the default type used when creating a new store. Data is simply stored in a data var and is lost on unload (close window, leave page, etc.) This constructor is instantiated when the "DataManager.add()" method is called
Parameters:
Name Type Argument Default Description
storeName String the name used to reference this particular store
settings Object <optional>
{} the settings to be passed to the adapter
Properties
Name Type Argument Default Description
recordId String <optional>
"id" the name of the field used to uniquely identify a "record" in the data
Source:
Returns:
The created store
Type
Object
Example
// Create an empty DataManager
var dm = AeroGear.DataManager();

// Add a custom memory store
dm.add({
    name: "newStore",
    settings: {
        recordId: "customID"
    }
});

Methods

<static> isValid()

Determine if this adapter is supported in the current environment
Source:

filter(filterParameters, matchAny) → {Object}

Filter the current store's data
Parameters:
Name Type Argument Description
filterParameters Object <optional>
An object containing key/value pairs on which to filter the store's data. To filter a single parameter on multiple values, the value can be an object containing a data key with an Array of values to filter on and its own matchAny key that will override the global matchAny for that specific filter parameter.
matchAny Boolean <optional>
When true, an item is included in the output if any of the filter parameters is matched.
Source:
Returns:
A Promise
Type
Object
Example
    var dm = AeroGear.DataManager( "tasks" ).stores[ 0 ];

    / Create an empty DataManager
    var dm = AeroGear.DataManager();

    dm.open()
        .then( function() {

            // An object can be passed to filter the data
            // This would return all records with a user named 'admin' **AND** a date of '2012-08-01'
            dm.filter( {
                    date: "2012-08-01",
                    user: "admin"
                } )
                .then( function( filteredData ) { ... } )
                .catch( function( error ) { ... } );

            // The matchAny parameter changes the search to an OR operation
            // This would return all records with a user named 'admin' **OR** a date of '2012-08-01'
            dm.filter( {
                    date: "2012-08-01",
                    user: "admin"
                }, true )
                .then( function( filteredData ) { ... } )
                .catch( function( error ) { ... } );
        });

read(id) → {Object}

Read data from a store
Parameters:
Name Type Argument Description
id String | Number <optional>
Usually a String or Number representing a single "record" in the data set or if no id is specified, all data is returned
Source:
Returns:
A Promise
Type
Object
Example
    var dm = AeroGear.DataManager( "tasks" ).stores[ 0 ];

    // Get an array of all data in the store
    dm.read()
        .then( function( data ) {
            console.log( data );
        });

    // Read a specific piece of data based on an id
    dm.read( 12345 )
        .then( function( data ) {
            console.log( data );
        });

remove(toRemove) → {Object}

Removes data from the store
Parameters:
Name Type Description
toRemove String | Object | Array A variety of objects can be passed to remove to specify the item or if nothing is provided, all data is removed
Source:
Returns:
A Promise
Type
Object
Example
    var dm = AeroGear.DataManager( "tasks" ).stores[ 0 ];

    dm.open()
        .then( function() {

            // Delete a record
            dm.remove( 1, )
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );

            // Remove all data
            dm.remove( undefined )
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );

            // Delete all remaining data from the store
            dm.remove()
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );
        });

save(data, options) → {Object}

Saves data to the store, optionally clearing and resetting the data
Parameters:
Name Type Argument Default Description
data Object | Array An object or array of objects representing the data to be saved to the server. When doing an update, one of the key/value pairs in the object to update must be the `recordId` you set during creation of the store representing the unique identifier for a "record" in the data set.
options Object <optional>
{} options
Properties
Name Type Argument Description
reset Boolean <optional>
If true, this will empty the current data and set it to the data being saved
Source:
Returns:
A Promise
Type
Object
Example
    var dm = AeroGear.DataManager( "tasks" ).stores[ 0 ];

    dm.open()
        .then( function() {

            // save one record
            dm.save({
                    title: "Created Task",
                    date: "2012-07-13",
                    ...
                })
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );

            // save multiple records
            dm.save([
                    {
                        title: "Task2",
                        date: "2012-07-13"
                    },
                    {
                        title: "Task3",
                        date: "2012-07-13"
                        ...
                    }
                ])
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );

            // Update an existing piece of data
            var toUpdate = dm.read()[ 0 ];
            toUpdate.data.title = "Updated Task";
            dm.save( toUpdate )
                .then( function( newData ) { ... } )
                .catch( function( error ) { ... } );
        });