13 Sep 2012 1:48 PM #1
Controller's launch and init functions unexpectedly firing
I have a Sencha Touch 2 app with a single controller. That controller's init and launch functions fire as expected during the application's launch process. However, there are some situations in which they appear to fire again after that. One of the app's main views extends dataview.List, and presents the list using a template that causes it to render as a tiled list of thumbnail images. That view includes a listener that listens for tap events on those thumbnail elements and fires a local onElementTap method, which in turn fires an event that's caught by the controller and used to populate a detail view and make it the active item in the viewport. For most of the elements in the list, this works fine, but for the first one, tapping the element seems to also fire the controller's init and launch methods (usually, but not always). Stranger yet, tapping the second element in the list works correctly if I tap in the middle of the thumbnail, but has the same odd controller effect if I tap near the left edge of the thumbnail.
There is no place in my code where I attempt to forcibly fire the controller's launch or init functions, or to create a new instance of the controller class. Anybody got any ideas what bizarre thing I might have accidentally done that would cause this behavior?
16 Sep 2012 4:39 PM #2
- Join Date
- Mar 2007
- Gainesville, FL
- Vote Rating
The only place that executes the controller's init and launch method is Ext.application and there is only one spot that will do this.Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Software Engineer
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services firstname.lastname@example.org
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!
When posting code, please use BBCode's CODE tags.
17 Sep 2012 6:12 AM #3
Well, I think I have this fixed, although I'm still somewhat confused. My onElementTap method was changing which view was active in the viewport, from a list view to a detail view for the item whose element was tapped. The template for that detail view includes a larger image, which was wrapped in an anchor tag whose href inadvertently referenced an attribute name that doesn't exist in the definition of the underlying object. Clicking that image had the effect of reloading the page, effectively relaunching the whole app and causing the behavior I saw.
So, I could understand if I manually tapped on the image on the detail page and the app restarted. But what seemed to be happening is that sometimes (but not always) a tap on the list page would fire *two* events; the console.log statement in my onElementTap method would be executed twice. The first one would switch views like it was supposed to, but then the second one would get processed, and if the tap was in the section of the screen where the detail page image was located, it'd behave like I manually tapped that image, and the app would restart. That's why certain thumbnails never had the problem -- even if a tap on one of those images resulted in two events, the second one was on a part of the detail page where it didn't do anything.
I removed the anchor tag from the detail page altogether, and now the app behaves just fine. I figured that'd fix things because the second tap event wouldn't cause any issues when it happened, but oddly enough, the second tap event no longer happens at all; my onElementTap logging now appears in the console only once, like it should.