/* AeroGear JavaScript Library
* https://github.com/aerogear/aerogear-js
* JBoss, Home of Professional Open Source
* Copyright Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
A collection of data connections (stores) and their corresponding data models. This object provides a standard way to interact with client side data no matter the data format or storage mechanism used.
@status Stable
@class
@augments AeroGear.Core
@param {String|Array|Object} [config] - A configuration for the store(s) being created along with the DataManager. If an object or array containing objects is used, the objects can have the following properties:
@param {String} config.name - the name that the store will later be referenced by
@param {String} [config.type="Memory"] - the type of store as determined by the adapter used
@param {String} [config.recordId="id"] - @deprecated the identifier used to denote the unique id for each record in the data associated with this store
@param {Object} [config.settings={}] - the settings to be passed to the adapter. For specific settings, see the documentation for the adapter you are using.
@param {Boolean} [config.settings.fallback=true] - falling back to a supported adapter is on by default, to opt-out, set this setting to false
@param {Array} [config.settings.preferred] - a list of preferred adapters to try when falling back. Defaults to [ "IndexedDB", "WebSQL", "SessionLocal", "Memory" ]
@returns {object} dataManager - The created DataManager containing any stores that may have been created
@example
// Create an empty DataManager
var dm = AeroGear.DataManager();
// Create a single store using the default adapter
var dm2 = AeroGear.DataManager( "tasks" );
// Create multiple stores using the default adapter
var dm3 = AeroGear.DataManager( [ "tasks", "projects" ] );
// Create a custom store
var dm3 = AeroGear.DataManager({
name: "mySessionStorage",
type: "SessionLocal",
id: "customID"
});
// Create multiple custom stores
var dm4 = AeroGear.DataManager([
{
name: "mySessionStorage",
type: "SessionLocal",
id: "customID"
},
{
name: "mySessionStorage2",
type: "SessionLocal",
id: "otherId",
settings: { ... }
}
]);
*/
AeroGear.DataManager = function( config ) {
// Allow instantiation without using new
if ( !( this instanceof AeroGear.DataManager ) ) {
return new AeroGear.DataManager( config );
}
/**
This function is used by the AeroGear.DataManager to add a new Object to its respective collection.
@name AeroGear.add
@method
@param {String|Array|Object} config - This can be a variety of types specifying how to create the object. See the particular constructor for the object calling .add for more info.
@returns {Object} The object containing the collection that was updated
*/
this.add = function( config ){
config = config || {};
var i, type, fallback, preferred, settings;
config = Array.isArray( config ) ? config : [ config ];
config = config.map( function( value ) {
settings = value.settings || {};
fallback = settings.fallback === false ? false : true;
if( fallback ) {
preferred = settings.preferred ? settings.preferred : AeroGear.DataManager.preferred;
if ( typeof value !== "string" ) {
type = value.type || "Memory";
if( !( type in AeroGear.DataManager.validAdapters ) ) {
for( i = 0; i < preferred.length; i++ ) {
if( preferred[ i ] in AeroGear.DataManager.validAdapters ) {
// For Deprecation purposes in 1.3.0 will be removed in 1.4.0
if( type === "IndexedDB" || type === "WebSQL" ) {
value.settings = AeroGear.extend( value.settings || {}, { async: true } );
}
value.type = preferred[ i ];
return value;
}
}
}
}
}
return value;
}, this );
AeroGear.Core.call( this );
this.add( config );
// Put back DataManager.add
this.add = this._add;
};
// Save a reference to DataManager.add to put back later
this._add = this.add;
/**
This function is used internally by datamanager to remove an Object from the respective collection.
@name AeroGear.remove
@method
@param {String|String[]|Object[]|Object} config - This can be a variety of types specifying how to remove the object.
@returns {Object} The object containing the collection that was updated
*/
this.remove = function( config ){
AeroGear.Core.call( this );
this.remove( config );
// Put back DataManager.remove
this.remove = this._remove;
};
// Save a reference to DataManager.remove to put back later
this._remove = this.remove;
this.lib = "DataManager";
this.type = config ? config.type || "Memory" : "Memory";
/**
The name used to reference the collection of data store instances created from the adapters
@memberOf AeroGear.DataManager
@type Object
@default stores
*/
this.collectionName = "stores";
this.add( config );
};
AeroGear.DataManager.prototype = AeroGear.Core;
AeroGear.DataManager.constructor = AeroGear.DataManager;
/**
Stores the valid adapters
*/
AeroGear.DataManager.validAdapters = {};
/**
preferred adapters for the fallback strategy
*/
AeroGear.DataManager.preferred = [ "IndexedDB", "WebSQL", "SessionLocal", "Memory" ];
/**
Method to determine and store what adapters are valid for this environment
*/
AeroGear.DataManager.validateAdapter = function( id, obj ) {
if( obj.isValid() ) {
AeroGear.DataManager.validAdapters[ id ] = obj;
}
};
/**
The adapters object is provided so that adapters can be added to the AeroGear.DataManager namespace dynamically and still be accessible to the add method
@augments AeroGear.DataManager
*/
AeroGear.DataManager.adapters = {};
// Constants
AeroGear.DataManager.STATUS_NEW = 1;
AeroGear.DataManager.STATUS_MODIFIED = 2;
AeroGear.DataManager.STATUS_REMOVED = 0;