【问题标题】:Adding controller dynamically into app.js controllers: [] - Ext-JS 4将控制器动态添加到 app.js 控制器中:[] - Ext-JS 4
【发布时间】:2012-12-04 21:59:18
【问题描述】:

我有一个很大的应用程序,所以我没有在我的 app.js 中添加东西:

stores: []
controllers: []
views: []
models: []

其中只有我需要创建应用程序的东西。那么,当我单击节点(左侧面板)时,如何创建我需要的控制器并在该控制器中加载模型、视图、存储和其他内容?只调用控制器就够了吗(因为它们是在控制器中导入的)?

有点像

Ext.create('MyApp.path.SomeController');

是否会像我将其添加到 app.js 中的 controllers: [] 一样添加?

【问题讨论】:

    标签: javascript extjs controller extjs4


    【解决方案1】:

    来自我的 app.js,(因此 this 是一个 Ext JS 应用程序):

    addController: function (name) {
            var c = this.getController(name); //controller will be created automatically by name in this getter 
            //perform the same initialization steps as it would have during normal ExtJs process
            c.init(this);
            c.onLaunch(this);
        }
    

    name 是类名...

    请记住,您可以通过this.application从任何其他控制器获取应用程序实例的句柄

    【讨论】:

    • 必须是全名:'MyApp.file.controller' 还是只是'controller'?谢谢你的答案。 :)
    • 类全名:'MyApp.file.controller'
    • 由于我不能执行 .getController 方法,我怎样才能获得对我的应用程序的引用(就像你得到的那样)?
    • 在哪个范围内?在控制器范围内,您有 this.getApplication docs
    【解决方案2】:

    我的代码与 Jenson 的代码非常相似。

    // This function loads a controller dynamically and returns its first view
    // Note: We don't call onLaunch(this) here. This method might be called during 
    // bootstrap (like if there's a cookie with the recent page), after which 
    // the application itself will call onLaunch (once out of the Launch method).
    // The other issue is that the view is not added when this method is called
    // and we might need to reference the view withing onLaunch, so this is the
    // wrong place to call on Launch). Currently we're not relying on onLounch 
    // with controllers.
    dynamicallyLoadController: function( aControllerName )
    {
        // See if the controller was already loaded
        var iController = this.controllers.get(aControllerName);
    
        // If the controller was never loaded before
        if ( !iController )
        {    
            // Dynamically load the controller
            var iController = this.getController(aControllerName);
    
            // Manually initialise it
            iController.init();
        }
    
        return iController;
    },
    
    loadPage: function( aControllerName )
    {
        // save recent page in a controller
        Ext.util.Cookies.set( 'RecentPage', aControllerName );
    
        var iController   = this.dynamicallyLoadController( aControllerName ),
            iPage         = iController.view,
            iContentPanel = this.getContentPanel(),
            iPageIndex    = Ext.Array.indexOf(iContentPanel.items, iPage);
    
        // If the page was not added to the panel, add it.
        if ( iPageIndex == -1 )
            iContentPanel.add( iPage );
    
        // Select the current active page
        iContentPanel.getLayout().setActiveItem( iPage );
    },
    

    【讨论】:

    • 我注意到这个解决方案会导致每次创建视图时都应用控制器侦听器。例如,如果您创建一个面板,然后将其删除(使用自动销毁),然后创建一个新面板 - 所有侦听器都会触发两次。每次您执行此操作时,听众都会不断增加。
    • 我发现 MixedCollection me.controllers 从未添加过新控制器,因此它将重新初始化它。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多