Documentation
Primary version
Primary version
  • Cubbles documentation
  • First steps
    • Generate a project
    • Create a webpackage
    • Create an elementary component
    • Create a compound component
      • Compound slot initialization
  • Runtime extension - RTE
    • User guide
      • RTE Integration
      • The Cubbles TAG API
        • The Cubbles Dependency API
      • The Cubbles Javascript API
        • Interacting with Cubbles from the outside
        • Adding dynamic connections
        • Interacting with Elementary Cubbles from the inside
      • The RTE Processing
        • RTE initialization and rendering timeline
      • The Cubbles IFrame API
        • The Cubbles IFrame Resizer API
      • The Cubbles mutation based start event API
      • FAQs
        • How to manually resolve dependency conflicts?
        • How to create a component instance dynamically?
        • How to render HTML Markup coming from input slot?
        • How to replace a dependency when declaring a component instance?
        • How to synchronize multiple dataflows between component instances?
        • How to handle the copyValue flag for non serializable instances?
    • Contributor guide
      • CIF processing
  • Developing with the vanilla boilerplate
    • Creating a new project
    • Developing elementaries using the vanilla boilerplate
    • Developing compounds using the vanilla boilerplate
    • Using the vanilla boilerplate scripts
  • Coder devtools - CDT
    • User guide
      • Create Cubbles artifacts
      • Upload a Webpackage
      • Generate a README file
      • Rename an artifact
      • Configure network proxy
      • Validate the manifest.webpackage file
      • Change active webpackage
      • Bulk upload of webpackages
      • Release a webpackage
      • Update the RTE version of a webpackage
      • Prepare a webpackage to be released
      • Update webpackage to the next development version
      • Generate a test environment for a component
      • Testing Cubbles components
      • Validate sources
      • Create a demo webpackage
    • Contributor guide
      • Checklist for releasing a new webpackage.modelVersion
  • Terms and concepts
    • Webpackage
    • Artifacts
    • User roles
    • Base
Powered by GitBook
On this page
  1. Runtime extension - RTE
  2. User guide
  3. FAQs

How to create a component instance dynamically?

To create, and then, add a component instance dynamically you should:

  1. Add the dependency of the desired component to the rootDependencies

  2. Create the component using the document API method createElement()

  3. Append the component to the Cubbles container (by default the body, otherwise the element having the cubx-core-crc attribute)

The code below presents this process for adding a new cubx-textarea to an existing app:

<head>
    ...
    <script src="https://cubbles.world/sandbox/cubx.core.rte@3.0.0-SNAPSHOT/webcomponents/custom-elements-es5-adapter.js"></script>
    <script src="https://cubbles.world/sandbox/cubx.core.rte@3.0.0-SNAPSHOT/webcomponents/webcomponents-lite.js"></script>
    <script>
          // 1. You should add the dependency of the desired component to the rootDependencies
          window.cubx = {
            "CRCInit": {
            "rootDependencies": [
                   {
                      "webpackageId": "com.incowia.basic-html-components@2.0.0",
                      "artifactId": "cubx-textarea"
                    }
                ]
            }
          }
    </script>
    <script src="https://cubbles.world/sandbox/cubx.core.rte@3.0.0-SNAPSHOT/crc-loader/js/main.js" data-crcinit-loadcif="true"></script>
</head>


<body>
    ...
    <script>
        // 2. Create the component using the document API method createElement()
        var cubxTextArea = document.createElement('cubx-textarea');


        // 3. Append the component to the Cubbles container
        document.body.appendChild(cubxTextArea);
    </script>
</body>

Use document.createElement: Although you may want to append the component using the innerHTML property of a container, you should use the document.createElement() method for everything to work properly.

PreviousHow to manually resolve dependency conflicts?NextHow to render HTML Markup coming from input slot?

Last updated 6 years ago

You may also be interested in defining inits and connections for the component before appending it to the DOM. Check the and the to get more information about it.

The Cubbles Tag API
The Cubbles Javascript API