将控制器动态添加到app.js控制器中:[] - Ext-JS 4

时间:2012-12-04 21:59:19

标签: javascript extjs controller extjs4

我有一个很棒的应用,所以我没有在app.js中添加内容:

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

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

这样的东西
Ext.create('MyApp.path.SomeController');

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

2 个答案:

答案 0 :(得分:3)

从我的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

从任何其他控制器获取应用程序实例的句柄

答案 1 :(得分:2)

我的代码与简森的代码非常相似。

// 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 );
},