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