Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
How do a retrieve and run a function with require()?

I currently have the following impedance mismatch: an event handler for a dynamically placed control inside a reusable cshtml component needs to call a function loaded by require().

Is there a way to retrieve a module from require and have my lambda be invoked when it's ready?

<input type='checkbox' @(condition
    ? "onclick='require(\"js/view/handlers\",
        function (handlers) => {
             handlers.handleCheckboxMatchingCondition(this);
        });
        return true;'"
    : "")>

Normally the routine is to use id= and match up in the module initializer with addEventListener, but it won't work here because reusable component.

The idea isn't actually to do a new javascript load of a module (the module is loaded at the top of the page) but handle the possible condition of the user clicks the box before the module has finished loading yet.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So it turned out this code was very close to actually being working code. require() would only need a minor tweak to make it work.

<input type='checkbox' onclick="@(condition
    ? @"return (function(){
        let sync = true;
        require(['js/view/handlers'],
        function (handlers) => {
             if (sync)
                 // It's too hard to make this function work both sync and async
                 // so we kick it to top level and it's always async
                 setTimeout(0, handlers.handleCheckboxMatchingCondition, this);
             else
                 handlers.handleCheckboxMatchingCondition(this);
        });
        sync = false;
        return true;});"
    : "")>

Feels like something that could go into a trivial wrapper library (that is loaded directly for the obvious reason).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda