Foros del Web » Programando para Internet » Javascript »

persistencia de referencia a objetos, en handler de eventos

Estas en el tema de persistencia de referencia a objetos, en handler de eventos en el foro de Javascript en Foros del Web. Hola. Tengo un pequelo problema tengo un objeto al cual le eh colgado un handler de evento el cual me crear un div con objetos ...
  #1 (permalink)  
Antiguo 03/04/2009, 12:08
Avatar de ugmaster  
Fecha de Ingreso: abril-2008
Ubicación: Veracruz, México
Mensajes: 73
Antigüedad: 16 años
Puntos: 1
persistencia de referencia a objetos, en handler de eventos

Hola.

Tengo un pequelo problema tengo un objeto al cual le eh colgado un handler de evento el cual me crear un div con objetos dentro de el, cuyos objetos tambien tiene handler, pero el problema es que tengo la necesidad de saber el objeto inicial, es decir el objeto que disparo la creación del div, pero la peculiaridad es que lo necesito accesar desde el handeler de uno los objetos de la ventana generada por el primer handler.

Esta un poco complicado de explicar, el codigo va mas o menos asi
:
Código:
function click_seleccion_proveedor(e){
    div_oscuro= document.createElement('div');
    div_oscuro.setAttribute('id','div_oscuro');
    div_oscuro.setAttribute('class','div_oscuro');
    document.forms[0].appendChild(div_oscuro);  
    
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET','webservices_previo_compras_corp.py?action=selecciona_proveedor&cod_interno='+e.target.parentNode.parentNode.previousSibling.childNodes[1].childNodes[0].nodeValue,true);
    httpRequest.onreadystatechange= function () {
     if (httpRequest.readyState==4)
     {
     if(httpRequest.status==200)
     {
var xmldoc=httpRequest.responseXML;
var respuesta = xmldoc.getElementsByTagName('answer')[0].childNodes[0].nodeValue;
 if (respuesta=='si')
 {                                                                            
 div_nuevo_registro = document.createElement('div');                                                                            div_nuevo_registro.setAttribute('class','div_nuevo_registro');                                                                            div_nuevo_registro.setAttribute('id','div_nuevo_registro');
line1=document.createElement('h5');
line1.setAttribute('class','line1');                
input=document.createElement('input');
 input.setAttribute('name','cantidad');
input.setAttribute('id','cantidad');
input.setAttribute('type','text');
input.setAttribute('size','5');
input.setAttribute('maxlength','6');                input.setAttribute('value',e.target.parentNode.parentNode.childNodes[4].childNodes[0].nodeValue);
addEvent(input,'keypress',keypress_enteros,false);
addEvent(input,'keypress',keypress_esc_previo,false);
line1.appendChild(input);                
 input=document.createElement('input');
input.setAttribute('name','costo_unidad_compra');
input.setAttribute('id','costo_unidad_compra')
input.setAttribute('type','text');
input.setAttribute('size','5');
input.setAttribute('maxlength','6');                input.setAttribute('value',e.target.parentNode.parentNode.childNodes[5].childNodes[0].nodeValue);
addEvent(input,'keypress',keypress_numeros,false);
addEvent(input,'keypress',keypress_esc_previo,false);
line1.appendChild(input);       
select = document.createElement('select');
select.setAttribute('id','proveedor');
select.setAttribute('name','proveedor');       
option=document.createElement('option');
option.value=0;                           option.appendChild(document.createTextNode('Proveedor...'));
select.appendChild(option);                           addEvent(select,'keypress',keypress_esc_previo,false);
for(i=0;i<xmldoc.getElementsByTagName('proveedores')[0].childNodes.length;i++)
{
option=document.createElement('option');         option.value=xmldoc.getElementsByTagName('proveedores')0].childNodes[i].childNodes[1].childNodes[0].nodeValue;                                                                                    option.appendChild(document.createTextNode(xmldoc.getElementsByTagName('proveedores')[0].childNodes[i].childNodes[0].childNodes[0].nodeValue));
select.appendChild(option);
}
line1.appendChild(select)
div_nuevo_registro.appendChild(line1);
line2=document.createElement('h5');
line2.setAttribute('class','line2');
select = document.createElement('select');
 select.setAttribute('id','descuento');
select.setAttribute('name','descuento');
addEvent(select,'keypress',keypress_esc_previo,false);
for(i=0;i<xmldoc.getElementsByTagName('esquemas_descuentos')[0].childNodes.length;i++)
{
option=document.createElement('option');                                                                                  option.value=xmldoc.getElementsByTagName('esquemas_descuentos')[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;option.appendChild(document.createTextNode(xmldoc.getElementsByTagName('esquemas_descuentos')[0].childNodes[i].childNodes[0].childNodes[0].nodeValue));
select.appendChild(option);
}
line2.appendChild(select);
div_nuevo_registro.appendChild(line2);
                                                                            
span1= document.createElement('span');
span1.setAttribute('class','span1');               span1.appendChild(document.createTextNode("Paquetes"));
div_nuevo_registro.appendChild(span1);
                                                                            
span2= document.createElement('span');
span2.setAttribute('class','span2');
span2.appendChild(document.createTextNode("Costo U.C."));
div_nuevo_registro.appendChild(span2);
                                                                            
span3= document.createElement('span');
span3.setAttribute('class','span3');
                                                                            span3.appendChild(document.createTextNode("Proveedor"));
div_nuevo_registro.appendChild(span3);

span4= document.createElement('span');
span4.setAttribute('class','span4');
                                                                            span4.appendChild(document.createTextNode("Descuento"));
div_nuevo_registro.appendChild(span4);
                                                                            
span5 = document.createElement('span');
span5.setAttribute('class','span5');
image=document.createElement('img');
image.setAttribute('src','/apps/yepas/images/actions/apply.png');
                                                                            
span5.appendChild(image);
div_nuevo_registro.appendChild(span5);

document.forms[0].appendChild(div_nuevo_registro);
document.getElementById('cantidad').focus();
image.onClick=click_apply(e);
 }
else{
alert('No tiene proveedor para este articulo, por favor verifique su catalogo \n de proveedores');
document.forms[0].removeChild(div_oscuro);
}
 }
}
};
httpRequest.setRequestHeader('Content-Type','text/xml');
httpRequest.send(null);
}
EL primer handler click_seleccion_proveedor es disparado tras un click en un objeto IMG de un grid, como se puede observar dentro del código ocupo e.target para saber la fila donde se encuentra el objeto y asi poder acceder a las celdas aledañas, ahora bien el segundo handler click_apply lo quiero colgar al objeto img que es creado dentro del primer handler click_seleccion_proveedor, pero dentro de aplly lo que quiero hacer es regresar todos los datos editados al grid del cual inicio toda la cadena de eventos.

El problema es como obtengo la referencia al objeto que invoco al primer handler desde el segundo handler.

Puesto que en click_apply e.target se refiere a la img dentro del div creado en click_seleccion_proveedor, pero necesito accesar al e.target del handler click_seleccion_proveedor que es quien tiene la referencia a la img del grid el cual me ubica en la fila donde debo regresar los datos.

Eh buscado e intentando varias cosas pero aun no lo eh conseguido, espero y pueda ayudarme con esto.

Saludos.
__________________
"Lo que aun no existe... es porque el ser humano aún no lo ah imaginado..."
  #2 (permalink)  
Antiguo 03/04/2009, 12:09
Avatar de ugmaster  
Fecha de Ingreso: abril-2008
Ubicación: Veracruz, México
Mensajes: 73
Antigüedad: 16 años
Puntos: 1
Respuesta: persistencia de referencia a objetos, en handler de eventos

El codigo del handler apply
Código:
/**/
function click_apply(target)
    {
        if(document.getElementById('proveedor').selectedIndex>0)
            {
                if (trim(document.getElementById('cantidad').value).length>0)
                    {
                        if (trim(document.getElementById('costo_unidad_compra').value).length>0 && document.getElementById('costo_unidad_compra').value>0)
                            {
                                clear_Node(target.parentNode.parentNode.childNode[3]);
                                target.parentNode.parentNode.childNode[3].appendChild(document.createTextNode(target.parentNode.parentNode.childNode[0].nodeValue*document.getElementById('cantidad').value));
                                clear_Node(target.parentNode.parentNode.childNode[4]);
                                target.parentNode.parentNode.childNode[4].appendChild(document.createTextNode(document.getElementById('cantidad').value));
                                clear_Node(target.parentNode.parentNode.childNode[5]);
                                target.parentNode.parentNode.childNode[5].appendChild(document.createTextNode(document.getElementById('costo_unidad_compra').value));
                                clear_Node(target.parentNode.parentNode.childNode[6]);
                                target.parentNode.parentNode.childNode[6].appendChild(document.createTextNode(document.getElementById('proveedor').options[document.getElementById('proveedor').selectedIndex].text+';'+document.getElementById('esquemas_descuentos').options[document.getElementById('esquemas_descuentos').selectedIndex].value));

                                document.forms[0].removeChild(document.getElementById('div_oscuro'));
                                document.forms[0].removeChild(document.getElementById('div_nuevo_registro'));                
                            }
                        else{
                                alert('El costo no puede ser 0.00');
                                document.getElementById('costo_unidad_compra').value='0.00';
                                document.getElementById('costo_unidad_comopra').focu();
                            }
                    }
                else{
                        alert('Especifique una cantidad');
                        document.getElementById('cantidad').value='';
                        document.getElementById('cantidad').focu();
                    }
            }
        else{
                alert('Debe Seleccionar un proveedor');
                document.getElementById('proveedor').focus();
            }
    }
En un solo post no me cupo jejje perdon, espero puedan ayudarme con esto
__________________
"Lo que aun no existe... es porque el ser humano aún no lo ah imaginado..."
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:39.