PDA

View Full Version : Fire event in View



Urkman
27 Aug 2012, 1:59 AM
Hello,

currently I'm developing a new application using ExtJS 4.1.
And with this I have a problem, with firing a event in a View and catching this event in a Controller.

About the Application:
I have a viewport with a menu(toolbar) in the north region. The toolbar looks like this:


Ext.define('Tubu.view.Toolbar', {
extend: 'Ext.toolbar.Toolbar',
alias: 'widget.mainToolbar',
initComponent: function () {
this.addEvents('showUserList');
this.items = [
{
text: 'Users',
menu: {
items: [
{
text: 'New User'
},
{
text: 'User List',
handler: function () {
tmp = this.fireEvent('showUserList');
console.log(tmp);
}
}
]
}
}
]


this.callParent(arguments);
}
});

Here I fire the event "showUserList".

In my UserController i want to catch this event. Here is the Controller:


Ext.define('Tubu.controller.Users', {
extend: 'Ext.app.Controller',


stores: ['Users'],
models: ['User'],
views: ['user.List','user.Edit','Toolbar'],


init: function() {
this.control({
'mainToolbar': {
showUserList: this.doShowUserList
}
});
},


doShowUserList: function()
{
console.log('showUserList');
}
});

This are the simplified versions of the classes.

My Problem:
The event never occurs in the controller.

Where is my error here?

Thanks,
Stefan

vietits
27 Aug 2012, 5:50 AM
The problem is that you fire 'showUserList' from button 'User List', not from mainToolbar while your controller listens for this event from mainToolbar. Try to fix your code to fire event from mainToolbar or fix the selector in the controller to catch this event from button 'User List'.


Ext.define('Tubu.view.Toolbar', {
extend: 'Ext.toolbar.Toolbar',
alias: 'widget.mainToolbar',
initComponent: function () {
var me = this;
me.addEvents('showUserList');
me.items = [{
text: 'Users',
menu: {
items: [{
text: 'New User'
},{
text: 'User List',
handler: function () {
me.fireEvent('showUserList'); // fire event from mainToolbar, not from button
}
}]
}
}];
me.callParent(arguments);
}
});

Urkman
27 Aug 2012, 7:47 AM
Thanks. That's it...
But, why is the event not bubbled up?

vietits
27 Aug 2012, 5:11 PM
Thanks. That's it...
But, why is the event not bubbled up?

See my answer for your question at "Catching bubbled events in controllers (http://www.sencha.com/forum/showthread.php?174921-Catching-bubbled-events-in-controllers/page2)"