PDA

View Full Version : Want controllerB to listen to custom event fired from controllerA



estesbubba
27 Dec 2011, 1:34 PM
Two of us have to trying to figure this out and it seems like it should be simple. We have controllerA that has a custom event 'showDevelopmentMenu' and does a fireEvent on it. Here is a simplified view of what we're doing.





Ext.define('Csg.controller.A', {


extend: 'Ext.app.Controller',


config: {
bubbleEvents: [
'showDevelopmentMenu'
]
},


init: function() {
var me = this;


me.control({


'main #actionsButton': {
tap: me.onActionsButtonTap
}
});
},


onActionsButtonTap: function() {
var me = this;
me.fireEvent('showDevelopmentMenu');
}
});






Ext.define('Csg.controller.B', {


extend: 'Ext.app.Controller',


init: function() {
var me = this;


me.control({
showDevelopmentMenu: me.showDevelopmentMenu
// '*': {
// showDevelopmentMenu: me.showDevelopmentMenu
// }
});
},


showDevelopmentMenu: function() {
debugger;
}
});


I know the simple would would be have controllerB do a this.control() on the button but we have reasons for not doing that. Since controllerA creates the view that has the button we want it to fire a custom event that controllerB is listening for. We can see controllerA firing the event but controllerB doesn't get it. We've tried all kinds of things with this.control().

Any suggestions?

estesbubba
27 Dec 2011, 2:33 PM
Well I got it to work but don't know if it's the preferred way. Had to add this to controllerA



getBubbleTarget: function() {
return this.application;
},




and this to controllerB



init: function() {
var me = this;


// me.control({
// 'controller': {
// showDevelopmentMenu: me.showDevelopmentMenu
// }
// });
me.application.on({
showDevelopmentMenu: me.showDevelopmentMenu,
scope: me
});
},


Maybe me.control() only works for views?

estesbubba
30 Dec 2011, 12:37 PM
Update:

In controllerA we do


me.application.fireEvent('showDevelopmentMenu');


In controllerB we do



me.application.on({
showDevelopmentMenu: me.showDevelopmentMenu,
scope: me
});Doing it this way you don't need to enableBubble and getBubbleTarget()

linuxyf
30 Dec 2011, 7:32 PM
this.application.fireEvent

bstras21
5 Jan 2012, 10:21 AM
This works great but I have one question. How do I pass data to listener:

for example:

me.application.fireEvent('showDevelopmentMenu', 'testData');

Thanks!

estesbubba
5 Jan 2012, 10:36 AM
me.application.fireEvent('event', dataA, dataB, dataC);

onEvent: function(dataA, dataB, dataC);

bstras21
5 Jan 2012, 10:38 AM
As soon as I posted I got it:



this.application.on({
popup: function(type){
console.log('HERE IS THE TYPE' + type); },
scope: this
});

ryancanulla
1 Feb 2012, 9:55 PM
FYI - It's now



var me = this;
me.getApplication().on({
loginSuccess : me.onLoggedIn,
scope : me
});


And



var me = this;
me.getApplication().fireEvent('loginSuccess');