getController()不会加载文件,也不会触发init和lauch [英] getController() doesn't load file and doesn't fire init and lauch

查看:208
本文介绍了getController()不会加载文件,也不会触发init和lauch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在特殊情况下在运行时加载某些控制器.在此视频中, http://de.slideshare.net据说/senchainc/mvc-in-depth-part-2-tommy-maintz/可以使用getController做到这一点.如果此时未加载控制器,则文件已加载,并触发了控制器的初始化和启动.

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

var myController = this.getApplication().getController("MyController"); myController.test(); //-- Uncaught TypeError: Cannot call method 'test' of undefined

myController是未定义的,并且不会触发init和launch.

myController is undefined and init and launch are not fired.

sencha 2.2中是否不再提供此功能?我是否必须在应用程序的控制器"数组中包括所有控制器?在应用程序启动后,还有其他方法可以加载控制器吗?

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?

推荐答案

执行此操作有几个非常合理的理由,其中最重要的一点是并非您的所有应用程序都必须首先加载,这可以加快处理速度.少量.它还允许您仅根据用户权限或在用户会话期间执行的操作来加载应用程序的某些部分.在我的桌面应用程序中,我只有管理员用户才能看到的部分,因此我使用MyApp.app.getController('AppControllerAdministration')来获取类加载器以加载管理控制器.

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.

至少从我的经验来看,SashaZd的评论实际上是不正确的.我从应用程序的"controllers"数组中删除了控制器,并使用getController强制加载它.否则,它将在应用程序启动时出现,这是我认为OP试图避免的事情.至少在4.2.2中(使用Sencha Architect 3),对我有用的是还从app.js中删除对该控制器专有使用的任何商店,视图或模型的引用.然后,您可以将它们放在控制器的视图",模型"和存储"配置中(示例在发布末尾).因此,在我的应用程序中,如果我检查权限并且用户是管理员,则将创建用于管理UI的控制器,以及需要加载的所有商店,视图和模型.但是,直到我这样做,他们才在那里.您可以看到它们被加载到调试器中.

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.

注意:这意味着您可能需要显式加载商店或创建视图,因为可能不会自动创建它们.同样在开发模式下,ExtJS将推荐"您将它们放在app.js中,您只需忽略它们即可.诸如"[[Ext.Loader]同步加载'MyApp.controller.EditUserController';考虑在Ext.onReady上方添加Ext.require('MyApp.controller.EditUserController')"之类的消息实际上正是您想要的!

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!

您还可以根据用户操作选择加载控制器.我使用下拉菜单的值在AppControllerAdministration.js中进行此操作:

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),

要小心,您将要确保newController实际上已正确加载.

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'
    ]

这篇关于getController()不会加载文件,也不会触发init和lauch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆