Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/02/2012, 20:34
doylelives
 
Fecha de Ingreso: junio-2008
Ubicación: Capital Federal xD
Mensajes: 1.208
Antigüedad: 15 años, 10 meses
Puntos: 35
Problemas al setear valores dentro de closures

Hola a todos,
No puedo setear correctamente valores a propiedades de objetos dentro de closures.
Tengo 2 objetos (aves y peces) que poseen una propiedad que es un objeto llamado "control" el cual posee una variable "ownerId" , seteada a traves de init(), esta propiedad no parece ser seteada correctamente.
Código Javascript:
Ver original
  1. -------- Aves -------------------
  2. var aves = function() {
  3.   var controlAves = null;
  4.   var setAves = function() {
  5.     controlAves.init({
  6.         'ownerId':'aveContainer'
  7.     });
  8.     $('.aves').click(function(){
  9.         controlAves.getOwnerId();
  10.     });
  11.   };  
  12.   return {
  13.       setControl: function(ctr) {
  14.         controlAves = ctr;
  15.       },
  16.       init: function(){
  17.         setAves();
  18.       }
  19.   };
  20. }();
  21.  
  22. $(document).ready(function(){
  23.     aves.setControl(control);
  24.     aves.init();
  25. });
  26. ----------Peces------
  27. var peces = function() {
  28.   var controlPeces = null;
  29.   var setPez = function() {
  30.     controlPeces.init({
  31.         'ownerId':'pezContainer'
  32.     });
  33.     console.log(controlPeces.ownerId);
  34.     $('.peces').click(function(){
  35.         controlPeces.getOwnerId();
  36.     });
  37.   };  
  38.   return {
  39.       setControl: function(ctr) {
  40.         controlPeces = ctr;
  41.       },
  42.       init: function(){
  43.         setPez();
  44.       }
  45.   };
  46. }();
  47. $(document).ready(function(){
  48.     peces.setControl(control);
  49.     peces.init();
  50. });
  51. ----------control --------------
  52. var control = (function() {    
  53.     return {
  54.     ownerId:null,
  55.     getOwnerId:function(){
  56.        ////////Esta variable siempre es "pezContainer"
  57.         console.log(ownerId);
  58.     },
  59.     init: function(params) {
  60.       ownerId= params.ownerId;
  61.     }
  62.   };
  63. })();
Cada vez que hago click el metodo
controlPeces.getOwnerId();
siempre trae "pezContainer" como ownerId.
tanto en
$('.peces').click(function(){
como en
$('.aves').click(function(){
en lugar de mostrar "aveContainer" al hacer click en ".aves"
__________________
I am Doyle please insert code.

Última edición por doylelives; 17/02/2012 a las 07:06