-1

I have the following code to call an function on each button in a page.

       dojo.query('.btn').forEach(function(item){
        Obj =   new MyClass(item); // class calls the ajax request on error of each btn click I have to handle some functionality in below function showError
        dojo.connect(Obj, 'showError', dojo.hitch(Obj, function(errors){
            console.log(Obj + 'came');
        }));

Here I tried using dojo.hitch to maintain state of each object Obj, But it is not maintaining.Only first Obj is firing.For second Button also, the first Obj is firing.Is there anything I am missing here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user521024
  • 521
  • 2
  • 7
  • 29
  • Have you checked if your forEachLoop submits the correct items? That it loops through the hole bunch of Buttons you made or does it break at the first one? – MiBrock Sep 23 '13 at 08:44

1 Answers1

0

Obj is declared as a global variable. Try putting a var in front of it. For example:

var Obj = new MyClass(item);

If you don't provide the var, it will create a globally accessible variable called Obj. This means that each time you loop, you refer to the same global variable. The result of this is that each one is connected to the same Obj (and that's why both buttons are connected to the same object).

Also, read this question on StackOverflow to get a more detailed view about declaring variables and the global scope.

Community
  • 1
  • 1
g00glen00b
  • 41,995
  • 13
  • 95
  • 133