Page 1 of 1

[SOLVED] Stack Error : connection within foreach query

Posted: 09 August 2015, 22:17
by apollo1001
Hi All

I'm having a little trouble with a stack error coming from attempting to set up connections within a foreach query.

I am trying something like:

Code: Select all

dojo.query(".village").forEach(function(node, index, nodelist){ dojo.connect( node, 'onclick', this.onVillageClick ); });
I will hunt again for a solution in the morning, but if anyone has any insight on how to get around this, I would be very grateful.

Many thanks
Apollo

EDIT: have just stumbled across this forum post which looks promising.http://forum.boardgamearena.com/viewtop ... ach#p10871 Will let you know in the morning if it works!

Re: Stack Error : connection within foreach query

Posted: 09 August 2015, 22:53
by Rudolf
remember having the same trouble 2 years ago, someone advice me to have only one parameter element, and access it with fields: forEach(function(element) {... element.id...
also aware that it's only works with elements part of the DOM... And also as you did... check the answers my problem was solved...

long time not programming, i have to face up again all these problems too :)


Don't know if it helps ;)

Re: Stack Error : connection within foreach query

Posted: 10 August 2015, 02:11
by pikiou
IMO the problem comes from dojo.connect( node, 'onclick', this.onVillageClick );
In onVillageClick, "this" will reference the DOM element you clicked on.
I looked into your onVillageClick function and you want this to reference your game object, gameui.
So you should give onVillageClick the right binding for this to reference the right thing:

Code: Select all

dojo.connect( node, 'onclick', dojo.hitch( this, this.onVillageClick ) );

Re: Stack Error : connection within foreach query

Posted: 10 August 2015, 09:06
by apollo1001
Thank you for both responses. It was certainly a scope problem, but whilst pikiou's solution averted the stack error, the event never triggered on a click.

It is a crude work-around, but in case anyone else finds this thread with a similar problem, I am able to proceed using:

Code: Select all

var scope = this;
dojo.query(".village").forEach(function(node, index, nodelist){ dojo.connect( node, 'onclick', scope, 'onVillageClick' ); });