-
3 Nov 2011 2:27 PM #1
Answered: How to add button listener in panel with customer html layout
Answered: How to add button listener in panel with customer html layout
Hi,
I have an Ext.Panel that sets up its html property for a customer html layout. But how do I setup listeners to react on buttons or links.
Thanks,Code:Ext.define('app.view.Example', { extend: 'Ext.Panel', xtype: 'example', alias: 'example', config: { scrollable: true, html: '<div>'+ '<a id="buttonExample" href="#" class="buttonExample">do something</a>'+ '</div>' } }
Frank
-
Best Answer Posted by rdougan
That is because the control method uses ComponentQuery, and because you are just creating your button using HTML, it cannot find a component using that Id.
If you cannot use an actual Ext.Button, you will need to add custom logic into your view. I suggest you add a listener like suggested above, fire an event, and then listen to that event in your controller.
-
3 Nov 2011 4:46 PM #2
Something like this might work for you:
Code:initialize: function() { //... this.element.on({ scope : this, tap : 'onTap' }); this.callParent(arguments); }, onTap: function(e, btn) { //add your code here to check which button, etc... },
-
4 Nov 2011 2:02 PM #3
Hello heretic,
that works perfectly fine. Thanks a lot.
However, I am also trying something else. Following Tommy Maintz' idea of an MVC architecture that separates view and controller, the code that is listening for buttons and links should ideally be located in the controller (unless it is view-only stuff). So I am trying this...
app/app.js
app/controller/MyController.jsCode:Ext.application({ name : 'app', controllers: ['MyController'], ... });
app/view/MyView.jsCode:Ext.define('app.controller.MyController', { extend: 'Ext.app.Controller', views: ['MyView'], init: function() { this.control({ '#mybutton': { click: this.dosomething, } }); }, dosomething: function() { Ext.Msg.alert('Title', 'Yippee', Ext.emptyFn); } });
Unfortunately, the listener code is not being called. What am I doing wrong?Code:Ext.define('app.view.MyView', { extend: 'Ext.Panel', xtype: 'myview', alias: 'myview', config: { scrollable: true, html: '<div>'+ '<a id="mybutton" href="#" class="mybutton">do something</a>'+ '</div>' } });
Or is there any reason why this approach would not work with custom html panels?
Cheers,
Frank
-
5 Nov 2011 10:14 AM #4
That is because the control method uses ComponentQuery, and because you are just creating your button using HTML, it cannot find a component using that Id.
If you cannot use an actual Ext.Button, you will need to add custom logic into your view. I suggest you add a listener like suggested above, fire an event, and then listen to that event in your controller.


Reply With Quote