Hi everybody !
I need to use the same event "onChangeSelection" on the same stock component (this.stock.hand[this.my_id]) for two different game states:
"turn0"is the first play state. When I reach it and the player triggers the event, all is OK.
"playerTurn" is the common play state, each player get active on their turn for this state. But then when the player choose his card, it triggers both events 'action_clicForInitialMeld' and 'action_clicForMeld', displaying an error message for the first one since the game state is wrong. The action resulting after the second event is proceeded correctly.
I assume the problem is that the first listener has not been disconnected. I found nothing in BGA doc about disconnexion so I decided to put in place my own connect/disconnect mechanism based on dojo.connect and dojo.disconnect. The ideas behind that were found on the last topic of this thread http://stackoverflow.com/questions/8707 ... rs-in-dojo:
So I replace dojo.connect by this.on which calls it but getting track of all events being attached on the node:
And I make the disconnection after the ajax call:
Still OK for turn0 state, but for playerTurn state I get this message when the event is triggered:
What can I do to eliminate these error messages ?
Do you have a better way to manage event disconnection ?
Thanks
!
Tchebychev
I need to use the same event "onChangeSelection" on the same stock component (this.stock.hand[this.my_id]) for two different game states:
Code: Select all
onEnteringState: function( stateName, args )
{
switch( stateName )
{
case 'turn0':
console.log("turn0")
this.stock.hand[this.my_id].setSelectionMode(1);
dojo.connect(this.stock.hand[this.my_id], 'onChangeSelection', this, 'action_clicForInitialMeld' );
break;
case 'playerTurn':
this.stock.hand[this.my_id].setSelectionMode(1);
dojo.connect(this.stock.hand[this.my_id], 'onChangeSelection', this, 'action_clicForMeld' );
break;
}
},"playerTurn" is the common play state, each player get active on their turn for this state. But then when the player choose his card, it triggers both events 'action_clicForInitialMeld' and 'action_clicForMeld', displaying an error message for the first one since the game state is wrong. The action resulting after the second event is proceeded correctly.
I assume the problem is that the first listener has not been disconnected. I found nothing in BGA doc about disconnexion so I decided to put in place my own connect/disconnect mechanism based on dojo.connect and dojo.disconnect. The ideas behind that were found on the last topic of this thread http://stackoverflow.com/questions/8707 ... rs-in-dojo:
Code: Select all
///////////////////////////////////////////////////
//// Simple handler management system
// this.on replace dojo.connect
// this.off enables to disconnect all handlers on the object attached with this.on
on: function (obj, event, context, method, dontFix) {
if(obj._connectHandlers == undefined)
{
obj._connectHandlers = [];
}
var handler = dojo.connect(obj, event, context, method, dontFix);
obj._connectHandlers.push(handler);
return handler;
},
off: function (obj) {
if(obj._connectHandlers == undefined) {
return;
}
dojo.forEach(obj._connectHandlers, "dojo.disconnect(item)");
},Code: Select all
onEnteringState: function( stateName, args )
{
switch( stateName )
{
case 'turn0':
this.stock.hand[this.my_id].setSelectionMode(1);
this.on(this.stock.hand[this.my_id], 'onChangeSelection', this, 'action_clicForInitialMeld' )
break;
case 'playerTurn':
this.stock.hand[this.my_id].setSelectionMode(1);
this.on(this.stock.hand[this.my_id], 'onChangeSelection', this, 'action_clicForMeld' )
break
}
},And I make the disconnection after the ajax call:
Code: Select all
action_clicForInitialMeld : function(control_name) {
console.log('initial_meld')
if( !this.checkAction( 'initial_meld' ) ){
console.log('cheat_initial_meld')
return;
}
var card_id = this.stock.hand[this.my_id].getSelectedItems()[0].id;
this.ajaxcall( "/innovation/innovation/initial_meld.html",
{
lock: true,
player_id: this.my_id,
card_id: card_id
},
this, function(result){}, function(is_error){}
);
this.off(this.stock.hand[this.my_id]); //////////////////////// <- Here
this.stock.hand[this.my_id].setSelectionMode(0);
},
action_clicForMeld : function(control_name) {
console.log('meld')
if( !this.checkAction( 'meld' ) ){
console.log('cheat_meld')
return;
}
var card_id = this.stock.hand[this.my_id].getSelectedItems()[0].id;
this.ajaxcall( "/innovation/innovation/meld.html",
{
lock: true,
player_id: this.my_id,
card_id: card_id
},
this, function(result){}, function(is_error){}
);
this.off(this.stock.hand[this.my_id]); //////////////////////// <- And here
this.stock.hand[this.my_id].setSelectionMode(0);
},This error seems to be raised in the intern mechanism of 'onChangeSelection' on the stock component. But appart from that, the action is still resolved correctly.Javascript error:
During callback: onclick / onClickOnItem
_26c.advice is null
What can I do to eliminate these error messages ?
Do you have a better way to manage event disconnection ?
Thanks
Tchebychev