[SOLVED] Stack Error : connection within foreach query

Game development with Board Game Arena Studio
Post Reply
User avatar
apollo1001
Posts: 191
Joined: 21 July 2015, 10:41

[SOLVED] Stack Error : connection within foreach query

Post 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!
Last edited by apollo1001 on 10 August 2015, 09:08, edited 1 time in total.
User avatar
Rudolf
Posts: 566
Joined: 24 December 2011, 23:04

Re: Stack Error : connection within foreach query

Post 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 ;)
User avatar
pikiou
Posts: 389
Joined: 03 October 2011, 05:36

Re: Stack Error : connection within foreach query

Post 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 ) );
User avatar
apollo1001
Posts: 191
Joined: 21 July 2015, 10:41

Re: Stack Error : connection within foreach query

Post 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' ); });
Post Reply

Return to “Developers”