-
17 Mar 2012 6:21 AM #1
Event Listener for Panel in Controller
Event Listener for Panel in Controller
Hi guys,
I've been trying to listen for a tap event on a panel, it works if I use this:
Then I tried to move the tap listener to my controller file:Code:Ext.define('MyApp.view.MyContainerOfPanel', {extend: 'Ext.Container', xtype: 'mycontainer',... {xtype: 'panel', id: 'panelToTap',listeners: {tap: {fn: function() {console.log("Tapped on this panel");}, element: 'element'},}, html: 'Tap on me please...',},
It didn't workCode:Ext.define('MyApp.controller.MyContainerOfPanel', {extend: 'Ext.app.Controller',... control: {'mycontainer #panelToTap': {tap: {fn: function() {console.log("try panel tap on Flight Summary");},element: 'element'},},}
Any thought?
-
17 Mar 2012 3:23 PM #2Sencha - Sencha Touch Dev Team
- Join Date
- Jul 2009
- Location
- Palo Alto, California
- Posts
- 469
- Vote Rating
- 9
Controller doesn't support element event delegation at the moment. A typical solution is relaying the element events to become component events. For example:
In your custom view:
In your controller:Code:Ext.define('My.custom.Container', { extend: 'Ext.Container', xtype: 'mycontainer', config: { // ... }, initialize: function() { this.relayEvents(this.element, ['tap']); } });
Code:control: { 'mycontainer': { tap: function() { // ... } } }Sencha Touch Lead Architect
-
18 Mar 2012 9:25 PM #3
If I have a html in the panel, and I have several <img> with certain id, how to detect the tap event on the <img>?
-
28 Mar 2012 2:46 PM #4
You can call getTarget() to determine if the image was tapped.
Code://in view initialize: function() { this.relayEvents(this.element, ['tap']); } //in controller control: { myPanel: { tap: 'onMyPanelTap' } } onMyPanelTap: function(a) { image = a.getTarget("#imageId"); if (image) { alert('you clicked on your image'); } }
-
11 May 2012 12:47 AM #5
@Jacky Nguyen: Thanks for pointing this out ...
The examples given on the docs site did not work for me at all until I added the
line to the view.Code:this.relayEvents(this.element, ['tap']);
What I quite understand, when would it actually work at all without this line?
In the controllers refs I define a ComponentQuery to get a handle on a certain component (Panel or what not). So I'm always working on a component.
The tap events will always occur on some sort of element (eg. an image inside a panel) though, not the panel itself.
So by default, clicks/taps would never arrive at the controller, wouldn't they?
Perhaps the docs at http://docs.sencha.com/touch/2-0/#!/...app.Controller should be updated.
All the best
wirtsi
-
23 Oct 2012 8:47 PM #6
Thanks for pointing this out. It really works!
-
19 Nov 2012 9:42 PM #7
Listen to a event within a controller for a Panel
Listen to a event within a controller for a Panel
Thanks for the information. Saved my time....


Reply With Quote