动态地在控制器中添加视图和存储 - Ext-JS 4.1.1a

时间:2021-06-23 19:42:12

I want to add views and stores in controller dynamically. So, I've had this:

我想动态地在控制器中添加视图和存储。所以,我有这个:

Ext.define('App.controller.MyController', {
    extend: 'Ext.app.Controller',
    stores: ['App.store.Users'],
    views: ['App.view.Users.Index'],

I'm creating this controller dynamically with:

我正在动态创建此控制器:

var controller = this.getController("Users");

How can I add store and views dynamically, something like:

如何动态添加商店和视图,例如:

var controller = this.getController(moduleID);

controller.stores = [];
controller.views = [];

controller.stores.push('App.store.Users');
controller.views.push('App.view.Users.Index');

But when I do that, it's not working. Console is telling me that Ext can't get "buffered from undefined" so I'm thinking that I have to do this with Ext.apply() or Ext.merge() or something like that to get getters and setters for stores.

但是,当我这样做时,它不起作用。控制台告诉我Ext不能“从未定义缓冲”,所以我认为我必须使用Ext.apply()或Ext.merge()或类似的东西来获取商店的getter和setter。

What do you think?

你怎么看?

EDIT for @asgoth:

编辑@asgoth:

When you use this.getController("nameOfController"); and if the controller doesn't exists, Ext-JS creates one for you. That's working 100% because when I console.log(controller); I'm getting data (and docs says that too). :)

当你使用this.getController(“nameOfController”);如果控制器不存在,Ext-JS会为您创建一个。这是100%的工作,因为当我在console.log(控制器);我正在获取数据(文档也说明了这一点)。 :)

1 个解决方案

#1


1  

You do not have that much choices, because you will need to have the arrays ready when you are instantiating the controller. By default this happens only once cause it should be managed by the Ext.app.Application Controller (instance).

您没有那么多选择,因为在实例化控制器时需要准备好阵列。默认情况下,这只会发生一次,因为它应该由Ext.app.Application Controller(实例)管理。

First point is that you cannot use the getController method here because it does not accept any additional configuration. So the easiest solution would be the implementation of your own getController method, slightly renamed to avoid overriding.

第一点是你不能在这里使用getController方法,因为它不接受任何其他配置。因此,最简单的解决方案是实现自己的getController方法,稍微重命名以避免覆盖。

here is a example one:

这是一个例子:

getControllerInstance: function(name, cfg) {
    var me = this.application,
        controllers = me.controllers,
        controller = controllers.get(name);

    if (!controller) {
        controller = Ext.create(me.getModuleClassName(name, 'controller'), Ext.ApplyIf({
            application: me,
            id: name
        },cfg);

        controllers.add(controller);
        if (me._initialized) {
            controller.doInit(me);
        }
    }

    return controller;
}

Please note that this variant does not add values to any array param instead it will override any any existing param!

请注意,此变体不会向任何数组参数添加值,而是覆盖任何现有的参数!

Also note that all your controller will need to inherit from the controller that has this method.

另请注意,所有控制器都需要从具有此方法的控制器继承。

#1


1  

You do not have that much choices, because you will need to have the arrays ready when you are instantiating the controller. By default this happens only once cause it should be managed by the Ext.app.Application Controller (instance).

您没有那么多选择,因为在实例化控制器时需要准备好阵列。默认情况下,这只会发生一次,因为它应该由Ext.app.Application Controller(实例)管理。

First point is that you cannot use the getController method here because it does not accept any additional configuration. So the easiest solution would be the implementation of your own getController method, slightly renamed to avoid overriding.

第一点是你不能在这里使用getController方法,因为它不接受任何其他配置。因此,最简单的解决方案是实现自己的getController方法,稍微重命名以避免覆盖。

here is a example one:

这是一个例子:

getControllerInstance: function(name, cfg) {
    var me = this.application,
        controllers = me.controllers,
        controller = controllers.get(name);

    if (!controller) {
        controller = Ext.create(me.getModuleClassName(name, 'controller'), Ext.ApplyIf({
            application: me,
            id: name
        },cfg);

        controllers.add(controller);
        if (me._initialized) {
            controller.doInit(me);
        }
    }

    return controller;
}

Please note that this variant does not add values to any array param instead it will override any any existing param!

请注意,此变体不会向任何数组参数添加值,而是覆盖任何现有的参数!

Also note that all your controller will need to inherit from the controller that has this method.

另请注意,所有控制器都需要从具有此方法的控制器继承。