Search code examples
extjsweb-componentextwebcomponents

Using ExtWebComponents how could I create a simple view?


I would like to composite several components and elements together, how could I do that using Sencha ExtWebComponents?

For example:

<div>View</div>
<ext-button></ext-button>

Solution

  • Here's is a simple example of how you could composite web components.

    Consuming the component:

    1. In the index.js, I have imported the button component and imported the component with import './SandboxViewComponent';

    2. In the index.html, I'm declaring my with <my-sandbox-view></my-sandboxy-view>.

    Source Below

    SandboxViewComponent.js

    import template from './SandboxViewComponent.html';
    
    class SandboxViewComponent extends HTMLElement {
    
      constructor() {
        super()
    
        this.innerHTML = template;
      }
    
      connectedCallback() {
        var me = this;
    
        // Add after the buttonEl.ext.el is instantiated
        setTimeout(() => {
          setTimeout(() => {
            me._addListeners();
          }, 0);
        }, 0);
      }
    
      disconnectedCallback() {
      }
    
      attributeChangedCallback(attrName, oldVal, newVal) {
      }
    
      _addListeners() {
        var buttonEl = this.querySelector("ext-button");
    
        button.ext.on("tap", function () {
            alert("tap 1 works");
        });
    
        button.ext.addListener("tap", function() {
            alert("tap 2 works");
        });
    
        buttonEl.ext.el.on('click', () => {
          alert('ext on click works');
        });
      }
    
    }
    window.customElements.define('my-sandbox-view', SandboxViewComponent);
    

    SandboxViewComponent.html

    <div>Sandbox View</div>
    
    <ext-button
            text="Button 1"
            shadow="true"
            arrowAlign="bottom"
            ></ext-button>