问题
i want to load some controllers at runtime only in special cases. In this video http://de.slideshare.net/senchainc/mvc-in-depth-part-2-tommy-maintz/ is said that you can do this with getController. If the controller is not loaded at this time the file is loaded and init and launch of the controller are fired.
But if i try this:
var myController = this.getApplication().getController("MyController");
myController.test(); //-- Uncaught TypeError: Cannot call method 'test' of undefined
myController is undefined and init and launch are not fired.
Is this feature not there anymore in sencha 2.2? Do i have to include ALL of the controllers in the "controllers" array of the application? Is there any other way to load controllers after application start?
回答1:
this.getApplication()
is not used in the several latest versions of Sencha Touch 2.x. To get the application instance you need to run:
yourAppName.app.getController(...)
回答2:
There are several very valid reason to do this, not the least of which is that not all of your app has to load initially, which can speed things up a bit. It also allows you to have some portions of your app only get loaded based on user permissions or actions taken during the user's session. In my desktop app I have sections that can only be seen by admin users, so I use MyApp.app.getController('AppControllerAdministration') to get the class loader to load the administration controller.
The comment by SashaZd is actually not correct, at least in my experience. I remove the controller from the "controllers" array in the app and use getController to force it to load. Otherwise it comes in on app startup, which is what I think the OP was trying to avoid. At least in 4.2.2 (using Sencha Architect 3) what works for me is to also remove the reference to any store, view, or model that is exclusively used by that controller from app.js. You can then put them in the controller's "view", "model", and "store" configs (example at end of post). So in my application, if I check permissions and the user is an admin I create the controller for administration UI and all the stores, views, and models it needs get loaded as well. But they are not there until I do so. You can watch them get loaded in the debugger.
NOTE: This means that you may need to explicitly load your stores or create your views since they might not be created automatically. Also in development mode ExtJS will "recommend" you put them in app.js, which you'll need to just ignore. Messages like "[Ext.Loader] Synchronously loading 'MyApp.controller.EditUserController'; consider adding Ext.require('MyApp.controller.EditUserController') above Ext.onReady" are actually exactly what you want!
You can also choose to load controllers based on user action. I use the value of a dropdown to do this inside AppControllerAdministration.js:
var controllerName = 'MyApp.controller.Edit' + resourceType + 'Controller',
newController = this.application.getController(controllerName),
To be careful, you will want to make sure newController actually got loaded correctly.
By way of an example of all this, here is the beginning of the declaration of my controller:
Ext.define('MyApp.controller.AppControllerAdministration', {
extend: 'Ext.app.Controller',
models: [
'Resource',
'EditorSection',
'Permission',
'Role'
],
stores: [
'Resources',
'Roles',
'Users',
'Routes'
],
views: [
'EditRoles',
'EditUsers',
'EditRoutes'
]
回答3:
Have you added the controller to the list of controllers in your app.js file ??
Sample code ::
Ext.application({
name: 'myApp',
requires:[
'Ext.TitleBar'
],
controllers: ['myDefaultController', 'onlyWhenCalledController', 'otherRandomController'],
. . . .
});
Only if you do that, then you can use the getController function. Else the controller you're calling is undefined.
Besides that, of course, you'll need working code for your controller of the same name under the controller folder in your app. When both of the above are in your app, only then will the functions you're calling above work perfectly.
来源:https://stackoverflow.com/questions/16613014/getcontroller-doesnt-load-file-and-doesnt-fire-init-and-lauch