Utility function to get component template
Current Behavior
Getting the template for any given component requires invoking global.bl.componentRegistry.get(type)
then manually coping over the fields needed.
New Behavior
Expose a new utility function that only needs to be given the resource type of the component. The utility function will handle all necessary copying / processing to the template data.
Implementation
Expose a new utility function called componentTemplate
that is given a resource type and returns that component's template. This template can then be modified as needed and added dynamically to the model processing queue using $.componentInclude
(#20 (closed)).
componentTemplate
$.componentInclude(resourceType, options)
Description
This will load the template from the given components's .content.json and return it as a plan Javascript Object. Optionally, it can mangle the namespaces and stash meta content.
Arguments
resourceType (String) : The type of the component to lookup. If left blank an error will be thrown.
options (Object) : Optional. Supported options are as follows:
-
mangleNamespaces (Boolean): If true, the SlingConnector
mangleNamespaces
function will be invoked on request SlingConnector object with the generated template as a parameter. This will convert any properties that match the regex/(?!^bl:)(^\w{1,6}):(.*)/
to an underscore version. This function will recurse to all child objects. -
stash (Boolean) : If true, the SlingConnector
stashMeta
function will be invoked on request SlingConnector object with the generated template as a parameter. This will move any namespace mangled entries into a non-enumerable child object named _meta. As it's non-enumerable it won't appear when doing console.log or using for loops. It is, however, still directly accessible. This function will recurse to all child objects.
Example
Component 1: blacklight/core/components/random/.content.json
{
"jcr:primaryType": "nt:folder",
"jcr:content": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "blacklight/develop/component",
"title": "Some Random Component",
"template": {
"sling:resourceType": "foundation/components/parsys",
"jcr:primaryType": "nt:unstructured",
"child": {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured",
"grandChild" : {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured"
}
}
},
"dialog_fields": {
"sling:resourceType": "foundation/components/parsys",
"jcr:primaryType": "nt:unstructured"
},
"includes": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "foundation/components/parsys"
}
}
}
Component 2: blacklight/core/components/random2/raondom2.js
module.exports.process = (model, $) => {
const template0 = $.componentTemplate("blacklight/core/random");
const template1 = $.componentTemplate("blacklight/core/random", {mangleNamespaces: true});
const template2 = $.componentTemplate("blacklight/core/random", {stash: true});
const template3 = $.componentTemplate("blacklight/core/random", {mangleNamespaces: true, stash: true});
}
Results:
template0:
{
"sling:resourceType": "fshr/design3/content/test",
"child": {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured",
"grandChild": {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured"
}
}
}
template1:
{
"child": {
"grandChild": {
"_sling_resourceType": "blacklight/core/components/child",
"_jcr_primaryType": "nt:unstructured"
},
"_sling_resourceType": "blacklight/core/components/child",
"_jcr_primaryType": "nt:unstructured"
},
"_sling_resourceType": "fshr/design3/content/test"
}
template2:
{
"sling:resourceType": "fshr/design3/content/test",
"child": {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured",
"grandChild": {
"sling:resourceType": "blacklight/core/components/child",
"jcr:primaryType": "nt:unstructured"
}
}
}
template3:
{
"child": {
"grandChild": {}
}
}