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
  • The simplified cubx-textarea sample case
  • The view of our simplified cubx-textarea
  • The logic of an elementary
  • The CubxComponent object
  • Accessing the local DOM of an elementary
  • Adding logic to the cubx-textarea component
  • A working example
  • Interaction via initialization
  • A working example
  1. Runtime extension - RTE
  2. User guide
  3. The Cubbles Javascript API

Interacting with Elementary Cubbles from the inside

PreviousAdding dynamic connectionsNextThe RTE Processing

Last updated 6 years ago

You can control the behavior (logic) and view of an elementary component (remember that compound components have no associated logic). When you are developing elementary components you will certainly need to control its behavior; e.g., by setting a slot value, or performing some action after a slot value changes.

The simplified cubx-textarea sample case

For this tutorial, we will assume that we are developing our cubx-textarea component, we will explore how to interact with the elementary from the view, the logic and the manifest (See ). For the purpose of this tutorial we will assume that the cubx-textarea only have one input/output slot called value as illustrated below.

The view of our simplified cubx-textarea

Our simplified cubx-textarea will contain a normal textarea HTML element, which will be manipulated from the logic layer. The view of our elementary looks as follows:

<template id="cubx-textarea">
    <textarea></textarea>
</template>
// Include the logic of our elementary
<script src="cubx-textarea.js"></script>

The logic of an elementary

The CubxComponent object

The CubxComponent object provides the global CubxComponent() function to register a new Cubble component. It provides the model property to control the state of an elementary component. You should extend this object to code the logic of an elementary.

Method name convention

Description

created ()

Called when the local DOM of an elementary has been created

ready ()

Called when the local DOM of an elementary has been initialized

connected ()

Called when the HTML element of an elementary has been attached to the DOM

disconnected ()

Called when the HTML element of an elementary has been detached to the DOM

contextReady ()

Called when the Cubbles framework is ready to work; that is, when all dependencies are included, all components and all connections are created and the initialization is done.

model[SlotId]Changed ()

Called when the value of a slot has changed after calling the set[SlotId](value) method. It is useful when you need to perform additional logic after a slot has changed.

Accessing the local DOM of an elementary

You can access the local DOM of an elementary using the standard Javascript selection methods (this.querySelector(selectors) and this.querySelectorAll(selectors)). Additionally, you can use the this.$$(selectors) shortcut for this.querySelector(selectors). For instance, the code below will return the textarea element contained in our 'cubx-textarea' elementary:

var textarea = this.$$('textarea');

Adding logic to the cubx-textarea component

To make our cubx-textarea to work properly when the textarea value changes, we will we need extend the logic of our component with:

  1. A listener to the change event of the component (e.g. in the contextReady method).

  2. A method to update the textarea html when the value slot changes.

That extension can be done extending the CubxComponent object. In our case, we will do it in a javascript file (cubx-textarea.js), which was included in the view above.

(function () {
  'use strict';

  CubxComponent({
    is: 'cubx-textarea',

    // 1. 'change' event listener
    contextReady: function() {
      this.$$('textarea').addEventListener("change", function () {
        this.setValue(event.target.value);
      }.bind(this), false);
    },

    // 2. update textarea element when the 'value' slot changes
    updateValue: function (newValue) {
      if (newValue !== undefined) {
        this.$$('textarea').value = newValue;
      }
    },
  });
}());

A working example

Code

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>&lt;cubx-textarea&gt;</title>
    <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>
      window.cubx = {
        CRCInit: {
            rootDependencies: [
              {
                    webpackageId: 'com.incowia.basic-html-components@2.0.0-SNAPSHOT',
                    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>
  <cubx-textarea cubx-webpackage-id="com.incowia.basic-html-components@2.0.0-SNAPSHOT" id="textarea1">
    <cubx-core-init style="display:none">
        <cubx-core-slot-init slot="value">"Value of textarea 1"</cubx-core-slot-init>
    </cubx-core-init>
    <cubx-core-connections style="display:none">
        <cubx-core-connection connection-id="valueCon" source="value", destination="textarea2:value"></cubx-core-connection>
    </cubx-core-connections>
  </cubx-textarea>
  <cubx-textarea cubx-webpackage-id="com.incowia.basic-html-components@2.0.0-SNAPSHOT" id="textarea2"></cubx-textarea>
</body>

</html>

Result

Interaction via initialization

Input slots can be initialized in manifest, so you can pre-defined an initial interaction by default. For elementary components, the slot definition object has a property called value. This value will be set to the slot during component initialization; thus, when cubxReady() is called, the slot will have that value.

A working example

We will initialize the value of the slots of our cubx-textarea component. The slots definition should look similar to the one below, note that the initialization occurs due to the value property:

...
"slots": [
  {
    "slotId": "value",
    "type": "string",
    "direction": [
      "input",
      "output"
    ],
    "value": "The value of my textarea",
  }
]
...

Note that these initial values will not be propagated since this initialization is only valid for input slots.

This time, we should just use the component to see the result of defining init values for the slots:

Code

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>&lt;cubx-textarea&gt;</title>
    <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>
      window.cubx = {
        CRCInit: {
            rootDependencies: [
              {
                    webpackageId: 'com.incowia.basic-html-components@2.0.0-SNAPSHOT',
                    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>
  <cubx-textarea cubx-webpackage-id="com.incowia.basic-html-components@2.0.0-SNAPSHOT"></cubx-textarea>
</body>

</html>

Result

When programming the logic of an elementary component you will need to access and edit slots' values. You can also use the used to interact from the outside, i.e., get[SlotId](), set[SlotId](value), slots () and repropagate[SlotId] (). In that case, the component is referenced using this since you are inside the component, e.g. use this.getValue() in our cubx-textarea elementary to access the value of the value slot. Besides those methods, you can implement the ones described below:

We will use two instances of the cubx-textarea elementary. In this example, we use the to initialize the value slot of one the instances of the component and create a connection between both instances.

Check to see the result working online. Note that this demo uses the complete version of the cubx-textarea

The Cubbles Tag API
this demo
Create an Elementary Component
Interface of the simplified cubx-textarea elementary
Simplified cubs-text-area demo
cubx-textarea that was initialized from the manifest
public methods