Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Respond to an event in an ES6 module

I have a bit of a complex situation that I am trying to find a solution for.

I have a web app that has a single module that then imports other classes. These classes handle building the UI and doing MVC-type work.

I have one class that builds a display of DIV elements. The name field in the div is contentEditable and I have some events that I use to allow me to select the text and then set the element to not be contentEditable when the user hits enter or there is a blur event.

All of this works quite well but I want to call back to a class when the DIV element has been edited and I can't figure out how to do it.

So my hierarchy is

  • core.js - my main module
  • displayList.js - the UI builder imported
  • eventFunctions.js - imported by displayList to allow it to assign addListener events
  • makeItemNonEditable() - the function that gets called

In the makeItemNonEditable() function I want to call to displayList so that this list knows that the UI element has been updated.

I have created a customEvent and added the appropriate handlers to the displayList class but it doesn't respond to the event. The addListener event is triggered and the event is being propagated as I can respond to it from core.js but not from the imported displayList.js class.

import displayList from './displayList.js';
const objDisplayList = new displayList();

objDisplayList.addEventListener('nameChanged',objDisplayList.updateShipName);

Is there a way to do this or do I need to rework how I am propagating events?

Update

Just answering some of the questions below.

The custom event is being declared in eventFunctions.js and triggered as part of a blur event on the DIV the user is editing. So if the user causes a blur event or hits Enter then the nameChanged event is sent

const nameChange = new Event('nameChanged');
function makeItemNonEditable(event) {

    // set the item's contentEditable prop to false
    event.target.contentEditable = false;
    event.target.dispatchEvent(nameChange);
    
}

I am using the target of the blur event as the src of the event

makeItemNonEditable is called from an eventListener attached to the DIV when it is created

// first the name field
let nameField = lastItem.querySelector('.displayName');
nameField.addEventListener('click', makeItemEditable);
nameField.addEventListener('blur', makeItemNonEditable);
nameField.addEventListener('keydown', testForEnter);

The display object creates the div and then attaches the eventListeners to it.

I have tried several different methods to try to get the displayList class as a target. I have extended the EventTarget class and also added addEventListener methods to the class.

At no point, and with no method I try, does the class get added as an eventListener. So when I run my app in a browser and use the getAllEventListeners method in the console I can see that the custom event is being added and my test hooks to it are there but not for the displayList class.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So as @Bergi mentions in the comments the most direct way to address this issue is to add a property to the DOM element which is a reference to the displayList object.

That works and is such a simple answer that I am kicking myself for not seeing it earlier.

The makeItemNonEditable function can then use that property to call back to the displayList object and send the target reference as well to make it all simpler.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda