Page 1 of 1
every click gets called multiple times
Posted: 22 July 2020, 23:17
by Ginso
Hello,
i have elements that i connect to function using
Code: Select all
dojo.query('.card').connect('onclick',this,'onChooseCard');
in this function i stop the event:
Code: Select all
onChooseCard: function( evt ) {
dojo.stopEvent(evt);
//...
But when i click on this card, the function gets called n times, where n is the number of players.
Why is that?
Re: every click gets called multiple times
Posted: 22 July 2020, 23:43
by Tisaac
Maybe because you are actually calling the connect multiple time on the same dom element ?
Can you provide some more code ?
Re: every click gets called multiple times
Posted: 23 July 2020, 03:07
by paramesis
As Tisaac mentioned, you've probably connected the method multiple times. I would guess you accidentally have that call inside a for loop that iterates over each player.
If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
Re: every click gets called multiple times
Posted: 23 July 2020, 07:58
by Draasill
Also, "connectClass" may be adapted here ?
Code: Select all
this.connectClass(
'card,
'onclick',
'onCardClick'
)
Re: every click gets called multiple times
Posted: 23 July 2020, 08:20
by docthib
paramesis wrote:If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
This ^
If you're using dojo.connect() - in onEnteringState function for example - you'll need to dojo.disconnect() the element before "connecting" again.
Or test if you already created the connect before creating a new one.
Code: Select all
// in .js file
// this.connexions has been declared / initiated in constructor
if (this.connexions['undo_button'] == undefined) {
this.connexions['undo_button'] = dojo.connect($btnUndo, 'onclick', this, 'onClickUndoButton' );
}
Re: every click gets called multiple times
Posted: 23 July 2020, 10:04
by fafa-fr
Hi,
I don't know if your problem comes from the fact that you connect and disconnect your cards, but if it does: (can be useful for other devs, too)
paramesis wrote: ↑23 July 2020, 03:07
If for whatever reason you need to add listeners dynamically, dojo.connect creates an object that you would need to store so that you can use dojo.disconnect.
BGA implemented a this.connect() method that automatically stores the connection handler for you, so you don't need to store it yourself. Then you must use this.disconnect() to disconnect it. But I remember I found it had limitations (can't remember which ones, maybe the only thing is that it's less convenient than chaining dojo.query() and connect()). Not sure it's in the doc, so if you want to use this, you'll have to look into BGA's javascript or ask here (it doesn't have exactly the same arguments as dojo.connect). I used this a long time ago, so I hope I'm not saying incorrect things.
But to avoid troubles with connecting and disconnecting (and risking to connect twice by error), you can also consider connecting cards only once and for the whole game, to a method that will do something or not, depending on the context. This is my preferred way to deal with connections.
Re: every click gets called multiple times
Posted: 23 July 2020, 12:45
by docthib
fafa-fr wrote:you can also consider connecting cards only once and for the whole game
This is the best option unless you use the framework Undo feature (which calls the JS setup())