1. #1
    Sencha User
    Join Date
    Nov 2011
    Posts
    2
    Vote Rating
    0
    myok12 is on a distinguished road

      0  

    Default Unanswered: Instantiating controllers and views - manually or automatically?

    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.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,644
    Vote Rating
    899
    Answers
    3574
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    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 @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    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 services@sencha.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.

  3. #3
    Sencha User
    Join Date
    Nov 2011
    Posts
    2
    Vote Rating
    0
    myok12 is on a distinguished road

      0  

    Default


    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?

  4. #4
    Sencha User nak1's Avatar
    Join Date
    Jan 2008
    Posts
    266
    Vote Rating
    0
    Answers
    1
    nak1 is an unknown quantity at this point

      0  

    Default


    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

  5. #5
    Ext JS Premium Member
    Join Date
    Apr 2011
    Posts
    93
    Vote Rating
    3
    Answers
    1
    adammascherin is on a distinguished road

      0  

    Default


    Quote Originally Posted by myok12 View Post
    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?
    Could some elaborate on this idea? I want to do something similar (I think). I have 3 controllers and 3 views, each view should be associated with a controller.

    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?