Pregunta

Me preguntaba cómo activar programáticamente un evento de cambio con YUI3: agregué un detector de cambios a un nodo de cuadro de selección:

Y.get('#mynode').on('change', function(e) {
 Alert(“changed me”);
});

y en algún otro lugar del script desea activar ese evento. Funciona, por supuesto, cuando un usuario cambia el valor del cuadro de selección en el navegador. Pero he intentado muchas formas de dispararlo mediante programación, ninguna de las cuales ha funcionado. Incluyendo:

// All below give this error: T[X] is not a function (referring to what's called in .invoke(), // in the minified javascript
Y.get('#mynode').invoke('onchange');
Y.get('#mynode').invoke('change');
Y.get('#mynode').invoke('on','change');
Y.get('#mynode').invoke("on('change')");


/* Tried using .fire() which I found here: 
* http://developer.yahoo.com/yui/3/api/EventTarget.html#method_fire
* Nothing happens
*/

Y.get('#mynode').fire('change'); 

/* Looking around the APIs some more, I found node-event-simulate.js: 
 * http://developer.yahoo.com/yui/3/api/node-event-simulate.js.html, 
 * which by its name would seem to have what I want. I tried:
 * Error: simulate(): Event 'change' can't be simulated. 
 * ( (function(){var I={},B=new Date().getTim...if(B.isObject(G)){if(B.isArray(G)){E=1;\n)
 */

Y.get('#mynode').simulate('change');

¡Cualquier ayuda sería apreciada!

¿Fue útil?

Solución

YUI 3.0 no admite la simulación de los eventos change , como descubrió. Sin embargo, será compatible con YUI 3.1. Es en el tronco ahora.

Tu tercer intento:

Y.get('#mynode').simulate('change');

debería funcionar en 3.1.

edit

Parece que puede reemplazar la versión YUI 3.0 de event-simulate.js con la versión troncal, y funcionará en una aplicación 3.0. No he visto ningún problema hasta ahora.

Otros consejos

La solución habitual no es activar el evento mediante programación, sino mover toda la lógica del evento a una función y, en su lugar, llamar a esa función desde su código cuando corresponda.

Y.get('#mynode').on('change', function(e) {
    AlertUserOfChange();
});

function AlertUserOfChange()
{
    Alert(“changed me”);
}

¿Qué tal esto?

Y.Event.simulate('#mynode', 'change');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top