/workspace/mergeTemplates
The workspace is cached in the module scope to allow for the mergeObjectTemplates(layer) method to assign template objects defined in a JSON layer to the workspace.templates{}.
Requires
- module:/utils/roles
- module:/utils/merge
- module:/utils/envReplace
- module:/workspace/getTemplate
- module:/workspace/cache
Methods
(inner) assignWorkspaceTemplates(obj)
The method parses an object for a template object property.
The template property value will be assigned to the workspace.templates{} object matching the template key value.
The template._type property will be set to 'template' indicating that the templates origin is in the workspace.
It is possible to overassign _type:'core' templates which are loaded from the /mod/workspace/templates directory.
The method will call itself for nested objects.
| Name | Type | Description |
|---|---|---|
obj | Object |
(inner) getRoleContext(obj) → {Object}
Extracts the role-related properties from an object to provide a stable context for template processing.
Used in two ways:
- Captured from the parent object before iterating its templates[] array, preventing sibling templates from leaking roles into each other.
- Captured from a template before merging it into the parent, so that the template's own sub-templates are scoped to the template's roles rather than the accumulated parent roles.
| Name | Type | Description |
|---|---|---|
obj | Object | The object to extract role context from. |
A context object containing role, roles, localeRole, templateRole, and objRole.
- Type:
- Object
(inner) mergeObjectWithTemplate(obj, template) → {Object}
Merges a template into the parent object. Handles two cases:
- obj.template (singular): The object inherits from a single template. The obj is merged on top of the template.
- templates[] array item: A template from the array is merged into the existing object.
When a template from the templates[] array has its own nested sub-templates, the template's role context is captured before the merge. This prevents sibling template roles from leaking into the sub-template role combinations.
Similarly, when a template contributes a locales array, the template's role context is stored as localesRoleContext on the merged object. This allows getLocale to scope role combinations for nested locales to the template that defined them, rather than the fully accumulated parent.
| Name | Type | Description |
|---|---|---|
obj | Object | The parent object to merge the template into. |
template | Object | The resolved template object. |
An object containing the merged obj, any nextTemplates to process, and an optional nextTemplatesContext scoped to the template's roles.
- Type:
- Object
(async, inner) mergeTemplates(obj, rolesopt) → {Promise}
The mergeTemplates method will be called for a layer or locale obj.
The locale or layer object will be merged with a template defined as obj.template string property.
The method will check for a template matching the obj.key string property if obj.template is undefined.
An array of templates can be defined as obj.templates[]. The templates will be merged into the obj in the order the template keys are in the templates[] array.
| Name | Type | Attributes | Description |
|---|---|---|---|
obj | Object | ||
roles | array | <optional> | An array of user roles from request params. |
| Name | Type | Attributes | Description |
|---|---|---|---|
obj.template | string | <optional> | Key of template for the object. |
obj.key | string | Fallback for lookup of template if not an implicit property. | |
obj.templates | array | <optional> | An array of template keys to be merged into the object. |
The layer or locale provided as obj param.
- Type:
- Promise
(async, inner) objTemplate(obj, template, roles, contextopt) → {Promise.<Object>}
The method will request a template object from the getTemplate module method.
Possible error from the template fetch will be added to the obj.err[] array before the obj is returned.
The template will be checked against the request user roles.
The method will shortcircuit if roles restrict access to the template object.
Otherwise the obj will be merged into the template.
Templates defined in the obj.templates array will be merged into object.
| Name | Type | Attributes | Description |
|---|---|---|---|
obj | Object | ||
template | Object | The template maybe an object with a src property or a string. | |
roles | array | An array of user roles from request params. | |
context | Object | <optional> | Optional base roles for context. |
| Name | Type | Attributes | Description |
|---|---|---|---|
obj.template | string | <optional> | Key of template for the object. |
Returns the merged obj.
- Type:
- Promise.<Object>
(inner) prepareTemplate(obj, template, roles) → {Object}
Prepares a template for merging by applying role-based property overrides and filtering properties based on include/exclude lists.
| Name | Type | Description |
|---|---|---|
obj | Object | The parent object providing include/exclude property configuration. |
template | Object | The template to prepare. |
roles | Array | | User roles for role-specific property merging. |
The prepared template with role overrides applied and properties filtered.
- Type:
- Object
(async, inner) processRecursiveTemplates(obj, nextTemplatesopt, roles, nextTemplatesContextopt) → {Promise.<Object>}
Processes any remaining templates that need to be merged after the initial template merge.
If the merged object now has an obj.template property, it is processed as a single template inheritance.
If nextTemplates exist (from a template's own templates[] array), they are processed sequentially using the nextTemplatesContext. This scoped context ensures that sub-templates are combined only with their parent template's roles, preventing sibling template roles from polluting the role hierarchy.
For example, given templates: [demographics, stores] where stores has templates: [brand_a], the brand_a template will be combined with the stores role context only, not with the accumulated demographics roles.
| Name | Type | Attributes | Description |
|---|---|---|---|
obj | Object | The current merged object. | |
nextTemplates | Array | <optional> | Templates to process recursively. |
roles | Array | | User roles or true for admin. | |
nextTemplatesContext | Object | <optional> | Role context scoped to the parent template, used to prevent sibling role leakage. |
The fully merged object.
- Type:
- Promise.<Object>
(inner) templateProperties(template) → {Object}
The method checks whether the template object has an array property include_props and will iterate through the string entries in the array to remove all other properties from the template object.
Properties defined in the template object exclude_props array property will removed from the template object.
| Name | Type | Description |
|---|---|---|
template | Object |
| Name | Type | Description |
|---|---|---|
template.include_props | array | Remove all but these properties from template object. |
template.exclude_props | array | Remove these properties from template object. |
template
- Type:
- Object