Important

You are browsing the documentation for version 3.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Registry

A registry is a singleton service that allows to share some instances with unique identifiers (with property globalId). It takes care of the lifecycle for shared instances, and collects information about those who requested the instance and removes it once the list of applicants gets empty.

Registry API

The put method is used to add the objects with the unique globalId value to the registry. The second parameter, applicant, is an instance of PageComponent, Model, Collection, View or any other instance that has life cycle and triggers dispose event at the end.

It is used by the registry to preserve only the objects (instances, passed in a first parameter) that are still in use by any applicant. If all the applicants for the object are disposed, the registry disposes the object as well.

1 /**
2  * Puts instance into registry
3  *
4  * @param {{globalId: string}} instance
5  * @param {RegistryApplicant} applicant
6  * @throws {Error} invalid instance or instance already exists in registry
7  */
8 put: function(instance, applicant) { ... }

The globalId is also used to get the object from the registry:

1 /**
2  * Fetches instance from registry by globalId
3  *
4  * @param {string} globalId
5  * @param {RegistryApplicant} applicant
6  * @return {Object|null}
7  */
8 fetch: function(globalId, applicant) { ... }

It is pretty common case when these two methods are used together to fetch existing instance or create a new one and return it.

 1 var registry = require('oroui/js/app/services/registry');
 2 var BaseClass = require('oroui/js/base-class');
 3
 4 var Unique = BaseClass.extend({
 5     globalId: null,
 6     constructor: function(globalIds) {
 7         this.globalId = globalId;
 8     }
 9 }, {
10     getObject: function(globalId, applicant) {
11         var obj = registry.fetch(globalId, applicant);
12         if (!obj) {
13             obj = new Unique(globalId);
14             registry.put(obj, applicant);
15         }
16         return obj;
17     }
18 });
19
20 // and after, inside PageComponent
21
22 initialize: function(options) {
23     this.obj = Unique.getObject('uniqueKey', this);
24 }

There are two other methods that allow maintain the up-to-date information in the list of actual applicants:

 1 /**
 2  * Adds applicant relation to registry for instance
 3  *
 4  * @param {{globalId: string}} instance
 5  * @param {RegistryApplicant} applicant
 6  */
 7 retain: function(instance, applicant) { ... }
 8
 9 /**
10  * Removes applicant relation from registry for instance
11  *
12  * @param {{globalId: string}} instance
13  * @param {RegistryApplicant} applicant
14  */
15 relieve: function(instance, applicant) { ... }

If the instance was passed in the options and you need to preserve it in the property for the future use, the registry has to be notified that the new applicant holds the instance.

1     initialize: function(options) {
2         this.instance = options.instance;
3         registry.retain(this.instance, this);
4     }

When the applicant does not need a shared instance any more, it can notify the registry with relieve method:

1     disable: function() {
2         registry.relieve(this.instance, this);
3         delete this.instance;
4     }

Hint

See other methods documentation in registry.js