The data manager handles the beans and collections life cycle. It provides an API to declare data classes, instantiate them, and synchronize the data with the server. It relies on the data structure defined by the application metadata.
The data manager provides:
- An interface to declare bean and collection classes from metadata
- Factory methods for creating instances of beans and bean collections
- Factory methods for creating instances of bean relations and relation collections
- A custom implementation of
Backbone.sync
Data model metadata
The metadata is used to describe the data model. It contains information about module fields and relationships. From the following sample metadata, the data manager would declare two classes: Opportunities and Contacts.
var metadata = {
modules: {
Opportunities: {
fields: {
name: { ... },
...
}
},
Contacts: { ... }
},
relationships: {
opportunities_contacts: { ... },
...
}
};Working with beans
Declare bean classes from metadata payload.
declareModels should be called at application start-up and whenever the
metadata changes:
const DataManager = require('data/data-manager');
DataManager.declareModels(metadata);You may now create bean instances using factory methods.
var opportunity = DataManager.createBean(
'Opportunities',
{ name: 'Cool opportunity' }
);
// You can save a bean using the standard `Backbone.Model.save` method.
// The save method will use the data manager's sync method to communicate
// changes to the remote server.
opportunity.save();
// Create an empty collection of contacts.
var contacts = DataManager.createBeanCollection('Contacts');
// Fetch a list of contacts
contacts.fetch();Working with relationships
var attrs = {
firstName: 'John',
lastName: 'Smith',
// relationship field
opportunityRole: 'Influencer'
}
// Create a new instance of a contact related to an existing opportunity
var contact = DataManager.createRelatedBean(opportunity, null, 'contacts', attrs);
// This will save the contact and create the relationship
contact.save(null, { relate: true });
// Create an instance of contact collection related to an existing opportunity
var contacts = DataManager.createRelatedCollection(opportunity, 'contacts');
// This will fetch related contacts
contacts.fetch({ relate: true });
Members
-
<inner> beanCollection :function
-
Reference to the base bean collection class constructor. Defaults to Data/BeanCollection.
Type:
- function
-
<inner> beanModel :function
-
Reference to the base bean model class constructor. Defaults to Data/Bean.
Type:
- function
-
<inner> mixedBeanCollection :function
-
Reference to the base mixed bean collection class constructor. Defaults to Data/MixedBeanCollection.
Type:
- function
Methods
-
<inner> canHaveMany(module, link)
-
Checks if a given module can have multiple relationships via a given link.
Parameters:
Name Type Description modulestring Name of the module to do the check for.
linkstring Relationship link name.
Returns:
trueif the module's link is amany-type relationship,falseotherwise.- Type
- boolean
-
<inner> createBean(module [, attrs] [, options])
-
Creates an instance of a bean. Example of usage:
// Create an account bean. The account's name property will be set to // "Acme". const DataManager = require('data/data-manager'); var account = DataManager.createBean('Accounts', { name: 'Acme' }); // Create a team set bean with a given ID var teamSet = DataManager.createBean('TeamSets', { id: 'xyz' });Parameters:
Name Type Argument Description modulestring The module name.
attrsObject <optional>
Initial values of bean attributes, which will be set on the bean.
optionsObject <optional>
A hash of options.
Returns:
A new instance of a bean.
- Type
- Data/Bean
-
<inner> createBeanCollection(module [, models] [, options])
-
Creates instance of a bean collection. Example of usage:
const DataManager = require('data/data-manager'); // Creates an empty collection of account beans. var accounts = DataManager.createBeanCollection('Accounts');Parameters:
Name Type Argument Description modulestring The module name.
modelsArray.<Data/Bean> <optional>
Initial array or collection of models.
optionsObject <optional>
A hash of options.
Returns:
A new instance of a bean collection.
- Type
- Data/BeanCollection
-
<inner> createMixedBeanCollection( [models] [, options])
-
Creates a collection of beans of different modules.
Parameters:
Name Type Argument Description modelsArray | Data/BeanCollection <optional>
A list of models to populate the new collection with.
optionsObject <optional>
A hash of options.
Returns:
Collection of mixed modules collection.
-
<inner> createRelatedBean(bean1, beanOrId2, link [, attrs])
-
Creates an instance of related Data/Bean or updates an existing bean with link information.
// Create a new contact related to the given opportunity. const DataManager = require('data/data-manager'); var contact = DataManager.createRelatedBean(opportunity, '1', 'contacts', { 'first_name': 'John', 'last_name': 'Smith', 'contact_role': 'Decision Maker' }); contact.save(null, { relate: true });Parameters:
Name Type Argument Description bean1Data/Bean Instance of the first bean.
beanOrId2Data/Bean | string Instance or ID of the second bean. A new instance is created if this parameter is
null.linkstring Relationship link name.
attrsObject <optional>
Bean attributes hash.
Returns:
A new instance of the related bean or existing bean instance updated with relationship link information.
- Type
- Data/Bean
-
<inner> createRelatedCollection(bean, link [, models])
-
Creates a new instance of related bean collection.
// Create contacts collection for an existing opportunity. const DataManager = require('data/data-manager'); var contacts = DataManager.createRelatedCollection(opportunity, 'contacts'); contacts.fetch({ relate: true });The newly created collection is cached in the given bean instance.
Parameters:
Name Type Argument Description beanData/Bean Bean to link the related beans to.
linkstring Relationship link name.
modelsArray | Data/BeanCollection <optional>
An array of related beans to populate the newly created collection with.
Returns:
The created bean collection.
- Type
- Data/BeanCollection
-
<inner> declareCollectionClass(moduleName, platform [, collectionController])
-
Declares bean collection class for a given module.
Parameters:
Name Type Argument Description moduleNamestring The module name.
platformstring The platform name.
collectionControllerObject <optional>
The controller.
Returns:
The created class.
- Type
- function
-
<inner> declareModel(moduleName, module, platform [, modelController] [, collectionController])
-
Declares bean model and collection classes for a given module and caches them.
Parameters:
Name Type Argument Description moduleNamestring Module name.
moduleObject Module metadata object.
platformstring The platform name.
modelControllerObject <optional>
The bean controller to declare.
collectionControllerObject <optional>
The collection controller to declare.
-
<inner> declareModelClass(moduleName, module, platform [, modelController])
-
Declares the bean model class for a given module.
Parameters:
Name Type Argument Description moduleNamestring The module name.
modulestring The module metadata.
platformstring The platform name.
modelControllerObject <optional>
The model controller.
Returns:
The created class.
- Type
- function
-
<inner> declareModels( [modules])
-
Declares bean models and collections classes for each module definition.
Data manager uses Core.MetadataManager#getModules method to get metadata if
modulesparameter is not specified.Parameters:
Name Type Argument Description modulesObject <optional>
metadata hash in which keys are module names and values are module definitions.
-
<inner> getBeanClass(module)
-
Gets a bean class.
Parameters:
Name Type Description modulestring The module name to get the bean class from.
Returns:
The bean class for the given
module, orthis.beanModelif not found.- Type
- Object
-
<inner> getCollectionClasses()
-
Gets all collection classes.
Returns:
The hash of collection classes.
- Type
- Object
-
<inner> getEditableFields(model [, fields])
-
Gets editable fields.
Parameters:
Name Type Argument Description modelData/Bean | Data/BeanCollection The bean or collection to get fields from.
fieldsArray <optional>
Field names to be checked.
Returns:
Hash of editable fields.
- Type
- Object
-
<inner> getModelClasses()
-
Gets all bean classes.
Returns:
The hash of bean classes.
- Type
- Object
-
<inner> getOppositeLink(module, link)
-
Given a module and a link field name, this method gets the link field name of the other module of the relationship.
Parameters:
Name Type Description modulestring The module name.
linkstring The link name for the given module.
Returns:
oppositeLink The link field name of the other module of the relationship.
falseif not found.- Type
- string | boolean
-
<inner> getRelatedModule(module, link)
-
Gets related module name.
Parameters:
Name Type Description modulestring Name of the parent module.
linkstring Relationship link name.
Returns:
The name of the related module.
falseif not found.- Type
- string | boolean
-
<inner> getRelateFields(parentModuleName, link)
-
Gets fields of type
relatefor a given link.Example:
Assumptions:
- We have 2 modules
AccountsandCases. Accountshas a link field namedcases, which represents a 1-to-many relationship between the 2 modules: A case is linked to one account and a account can be linked to many cases.
To retrieve the field that matches this relationship on the Cases side, you can call:
var relateField = DataManager.getRelateField('Accounts', 'cases');and you would get the definition of the Cases field that exposes the parent Account bean.
Parameters:
Name Type Description parentModuleNamestring Name of the module that has a link field named
link.linkstring Link name.
Returns:
Definitions of the
relatefields if found or empty array if not found.- Type
- Array
- We have 2 modules
-
<inner> getRelationFields(module, link)
-
Gets relationship fields for a complex relationship.
Some relationships may have relationship fields, that only makes sense in the context of the relationship between 2 modules. Use the the following, to get the relationships fields definition of a relationship:
let relationshipFields = DataManager.getRelationFields('Opportunities', 'contacts');Parameters:
Name Type Description modulestring Name of the module that has a link field called
link.linkstring Link name.
Returns:
Relationship fields.
falseif no fields are found.- Type
- Array | boolean
-
<inner> getRelationshipFields(parentModule, link)
-
Gets relationship fields for a complex relationship.
Some relationships may have relationship fields, that only makes sense in the context of the relationship between 2 modules. Use the the following, to get the relationships fields definition of a relationship:
let relationshipFields = DataManager.getRelationshipFields('Opportunities', 'contacts');Parameters:
Name Type Description parentModulestring Name of the module that has a link field called
link.linkstring Link name.
- Deprecated:
-
- Yes
Returns:
Relationship fields.
- Type
- Array
-
<inner> getSyncAbortCallback(method, model [, options])
-
Gets the
abortcallback function for the sync #sync method.Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The model/collection to be synced/read.
optionsObject <optional>
A hash of options.
Properties
Name Type Argument Description abortObject <optional>
Custom
abortcallback function to be executed.Fires:
- data:sync:abort globally and on the bean/collection, if the sync request was aborted.event:
Returns:
The wrapped
abortcallback function.- Type
- function
-
<inner> getSyncCallbacks(method, model [, options])
-
Gets the
synccallback functions.Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The bean/collection to be synced/read.
optionsObject <optional>
A hash of options.
Returns:
A hash containing the fallowing callback functions: 'success', 'error', 'complete', 'abort'.
- Type
- Object
-
<inner> getSyncCompleteCallback(method, model [, options])
-
Gets the
completecallback function for the sync #sync method.Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The bean/collection to be synced/read.
optionsObject <optional>
A hash of options.
Properties
Name Type Argument Description completeObject <optional>
Custom
completecallback function to be executed.Fires:
- data:sync:complete globally and on the bean/collection, once the sync call was complete.event:
Returns:
The wrapped
completecallback function.- Type
- function
-
<inner> getSyncErrorCallback(method, model [, options])
-
Gets the
errorcallback function for the sync #sync method.Triggers the global
data:sync:completeevent (registered on SUGAR.App.events), as well as on themodel.Executes the abort callback if we are aborting from a previous collection fetch request.
Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The model/collection to be synced/read.
optionsObject <optional>
A hash of options.
Properties
Name Type Argument Description errorObject <optional>
Custom
errorcallback function to be executed.Fires:
- data:sync:error globally and on the bean, if the sync call returned an error.event:
Returns:
The wrapped
errorcallback function.- Type
- function
-
<inner> getSyncSuccessCallback(method, model [, options])
-
Gets the
successcallback function for the #sync method.Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The bean/collection to be synced/read.
optionsObject <optional>
A hash of options.
Fires:
- data:sync:success globally and on the bean, once the sync call is made and is successful.event:
Returns:
The
successcallback function.- Type
- function
-
<inner> init()
-
Initializes the data manager.
-
<inner> mergeModel(name [, module])
-
Merges a model with its metadata.
Parameters:
Name Type Argument Description namestring The module name.
moduleObject <optional>
The module metadata.
-
<inner> parseOptionsForSync(method, model [, options])
-
Builds and returns the options to pass to the
syncrequest.Parameters:
Name Type Argument Description methodstring The CRUD method.
modelData/Bean | Data/BeanCollection The bean/collection to be synced/read.
optionsObject <optional>
A hash of options.
Returns:
A hash of options.
- Type
- Object
-
<inner> reset( [module])
-
Resets class declarations.
Parameters:
Name Type Argument Description modulestring <optional>
The module name from which to remove the bean and collection class. If not specified, resets bean and collection classes of all modules.
-
<inner> resetCollection( [module])
-
Resets collection class declarations.
Parameters:
Name Type Argument Description modulestring <optional>
The module name from which to remove the collection class. If not specified, resets collection classes of all modules.
-
<inner> resetModel( [module])
-
Resets bean class declarations.
Parameters:
Name Type Argument Description modulestring <optional>
The module name from which to remove the bean class. If not specified, resets bean classes of all modules.
-
<inner> sync(method, model [, options])
-
Custom implementation of
Backbone.syncpattern. Syncs models with the remote server using SUGAR.Api.Parameters:
Name Type Argument Description methodstring The CRUD method: 'create', 'read', 'update', or 'delete'.
modelData/Bean | Data/BeanCollection The bean/collection to be synced/read.
optionsObject <optional>
A hash of options.
Fires:
- data:sync:start globally and on the bean, before the sync call is made.event:
Returns:
The sync request, or
falseif you attempted to load a linked context with no id on a linked bean.- Type
- Sugar.HttpRequest | boolean
Events
-
data:sync:abort
-
Fires on model when the sync operation ends.
Three parameters are passed to the callback:
- operation name (
method) - options
- request SUGAR.Api.HttpRequest
model.on('data:sync:abort', function(method, options, request) { SUGAR.App.logger.debug('Operation aborted ' + method + ' on ' + model); }); - operation name (
-
data:sync:abort
-
Fires when the sync operation was aborted.
Four parameters are passed to the callback:
- operation name (
method) - reference to the model/collection
- options
- request SUGAR.Api.HttpRequest
const Events = require('core/events'); SUGAR.App.events.on('data:sync:abort', function(method, model, options, request) { SUGAR.App.logger.debug('Operation aborted ' + method + ' on ' + model); }); - operation name (
-
data:sync:complete
-
Fires on model when the sync operation ends.
Three parameters are passed to the callback:
- operation name (
method) - options
- request SUGAR.Api.HttpRequest
model.on('data:sync:complete', function(method, options, request) { SUGAR.App.logger.debug('Finished operation ' + method + ' on ' + model); }); - operation name (
-
data:sync:complete
-
Fires when the sync operation ends.
Four parameters are passed to the callback:
- operation name (
method) - reference to the model/collection
- options
- request (SUGAR.Api.HttpRequest)
const Events = require('core/events'); Events.on('data:sync:complete', function(method, model, options, request) { SUGAR.App.logger.debug("Finished operation " + method + " on " + model); }); - operation name (
-
data:sync:error
-
Fires on model when the sync operation ends unsuccessfully.
Three parameters are passed to the callback:
- operation name (
method) - options
- error SUGAR.Api.HttpError
model.on('data:sync:error', function(method, options, error) { SUGAR.App.logger.debug('Operation failed:' + method + ' on ' + model); }); - operation name (
-
data:sync:error
-
Fires when the sync operation ends unsuccessfully.
Four parameters are passed to the callback:
- operation name (
method) - reference to the model/collection
- options
- error (SUGAR.Api.HttpError)
const Events = require('core/events'); Events.on('data:sync:error', function(method, model, options, error) { SUGAR.App.logger.debug('Operation failed ' + method + ' on ' + model); }); - operation name (
-
data:sync:start
-
Fires when the sync operation starts.
Three parameters are passed to the callback:
- operation name (
method) - reference to the model/collection
- options
const Events = require('core/events'); Events.on('data:sync:start', function(method, model, options) { SUGAR.App.logger.debug('Started operation ' + method + ' on ' + model); }); - operation name (
-
data:sync:start
-
Fires on model when the sync operation starts.
Two parameters are passed to the callback:
- operation name (
method) - options
model.on('data:sync:start', function(method, options) { SUGAR.App.logger.debug('Started operation ' + method + ' on ' + model); }); - operation name (
-
data:sync:success
-
Fires when the sync operation ends successfully.
Four parameters are passed to the callback:
- operation name (
method) - reference to the model/collection
- options
- request (SUGAR.Api.HttpRequest)
const Events = require('core/events'); Events.on('data:sync:success', function(method, model, options, request) { SUGAR.App.logger.debug('Finished operation ' + method + ' on ' + model); }); - operation name (