0
votes

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.

What do you think?

EDIT for @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). :)

1
With Ext.define() you are only defining your controller. You should create it with Ext.create() or new App.controller.MyController() - asgoth
Look the EDIT section. :) - Davor Lozic
Do you have a stacktrace or can you point where the error occurs? - asgoth
If I'm perfectly honest, what you are trying to do seems odd. The whole point in the oop design of the MVC framework is that controllers KNOW what views they control (and what models/stores). I can't help feeling there's a better way to achieve what you are trying to do. Could you please explain the rationale behind doing such a thing? - Izhaki
I'm with @Izhaki here, trying to add stores and views dynamically seems like you're trying to avoid needing to create explicit controllers. If that's the case, where are you intending to place your logic to control the views and user interactions? - dougajmcdonald

1 Answers

1
votes

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

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.

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.