23 Feb 2012 11:27 AM #1
Unanswered: Instantiating controllers and views - manually or automatically?
I have noticed many people are talking about MVC and creating [multiple] instances of controllers themselves, which makes total sense to me. I am also leaning toward creating controllers/views myself, but it looks counter intuitive to the approach of ST2, where you declare all your classes beforehand.
Let's consider an example: I have a large app with maybe 5 packed screens (each one is a carousel with many DOM elements including images). Since each one is large, and only one is shown at any given moment, I would want to be sure that only one of them fully exist in memory at any point in time -- when you navigate from one to the other, the previous one should be released. According to ST2 documentation and best practices, you should list all controllers and views in your Application class, and then they will all be dynamically loaded upon init (?), which doesn't sound like what I want to have (there's not a lot mentioned whether everything resides in memory throughout the app or if ST somehow releases and recreates views). What should I do? Should I, instead, instantiate the controllers myself (using Ext.create or Ext.widget? Or maybe have all the controllers set-up at init, but do not init the views until needed and released when not needed anymore? (something like KitchenSink, with its viewCache? although the code there looks so cumbersome for such a common task).
If I do go with the latter option (of creating, maintaining and releasing my controllers), would it mean that I can't use routes anymore? Also, will Sencha SDK not be able to compile my classes anymore, since I probably don't declare all my views and controllers in my Application (correct me if I'm wrong, please)? Seems like this is not as intended.
On a somewhat different note, why is it that in most of the examples the same controller is in charge of so many views? shouldn't there be more controllers? For example, the Speaker view in the Oreilly's app is being managed by the Speakers controller (which is also in charge of the Speakers view), the ProductView in the TouchStyle app is being managed by the Category controller, which also manages the ProductsList.
23 Feb 2012 12:29 PM #2
- Join Date
- Mar 2007
- Gainesville, FL
- Vote Rating
Having your controllers loaded up front is the proper way. You definitely should only have one controller instance present at any given time. You can use the requires to load the file but not instantiate them but if you have any routes or views configs in those controllers, things won't work as expected.Mitchell Simoens @LikelyMitch
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 email@example.com
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.
23 Feb 2012 1:17 PM #3
Can I have my controllers loaded up front, and have one controller instance present at any given time, at the same time?
How about loading all my controllers up front (using the controllers property of my Application class), and then having only one of the controllers to present its content at one time? I could do that if the controllers will have no meaningful instantiation code in init() and the relevant code will be in a separate method, that will perhaps be called by the route when needed?
For example, I might have an init(), show() and hide() methods. The init() will be called automatically by my Application. show() will be called by the route (or manually by me) when it should show its content and then would load the view and store it in a property. And then when I want to navigate elsewhere, I would call hide, which will destroy() the view and clear the property.
Is this more or less going the proper way?
29 Mar 2012 1:02 PM #4
A question on those routes, would that mean that the primary controller should be aware of the sub controllers? so that the routes would work properly?No longer a Newbie
24 Apr 2012 10:40 AM #5
The first view is hardcoded in a navigation view and using routes, other views are pushed to the navigation view. My problem is that once a view is pushed to the nav view, it doesn't seem to have any connection with the controller.
For example, Controller1 and View1 contain and control the navigation view. I have a View2 and a Controller2. Controller2 is loaded when the application loads and the init method gets called immediately. View2 is created using Ext.create() from Controller1 and pushed into the nav view only when a list item is clicked on the initial view (the itemtap event is in Controller1). In Controller2, I have some listeners for the objects in View2, but they don't get fired at all. Am I doing something wrong?