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

139
Visualizações
Can't communicate Alpine.js components due to inaccessible component context

I'm building a form (a razor page). There are two SelectList components, one depends on the other (see below).

While components were defined explicitly on the form page with alpine.js connected via cdn, productIdChanged event handler could access SelectList context (it had access to its disable(), enable() methods).

Since I've switched to webpack build the productIdChanged event handler's context changed to Window.

How to make these two communicate? I planned SelectList to be as much generic and reusable as possible.

<!-- this SelectList dispatches the event with product id -->

<div x-data="SelectList({ dispatchOnChange: true })" x-bind="ComponentRoot">
  <select x-ref="SelectInput" name="productId" id="ProductsList">
    <!-- ...options... -->
  </select>
</div>

<!-- this SelectList is expected to respond to the product id change -->

<div x-data="SelectList({
  eventHandlers: [
    {
      name: 'productIdChanged', 
      handler: function(e) {
        console.log([e, this]);
        // reset and then disable this select
      }
    }
  ] 
})" x-bind="ComponentRoot">
  <select x-ref="SelectInput" name="quantityUnitId" id="QuantityUnitsList">
    <!-- ...options... -->
  </select>
</div>

Here's the definition of the SelectList component, it's a selectr wrapper.

function SelectList({ customClass = '', searchable = false, dispatchOnChange = false, eventHandlers = [] } = {}) {
  return {
    init() {
      // assign event handlers
      eventHandlers.forEach(eh => { this.ComponentRoot[`@${eh.name}.window`] = eh.handler; });
      // configure selectr
      this.selectr = new Selectr(this.$refs.SelectInput, { ...customClass, searchable });
      // dispatch select value
      dispatchOnChange && this.selectr.on('selectr.change', (item) => {
        this.$dispatch(`${this.$refs.SelectInput.name}Changed`, { id: item.value });
      });    
    },
    reset()   { this.selectr.setValue(''); },
    disable() { this.selectr.disable(); },
    enable()  { this.selectr.enable(); },
    // used for x-bind
    ComponentRoot: { },    
  };    
}
about 4 years ago · Juan Pablo Isaza
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