2

Help! I have an ExtJS 4.2 MVC application that will contain multitudes of various Views. However, each user will have access to a mere fraction of the entire set. Obviously I don't want to load all of the Views for every user! Furthermore, each 'allowed' view should be loaded after the user has signed in.

In the past (before MVC) I used Ext.create() to dynamically load my .js class files. However, I'm trying to use the MVC design pattern and everything I've read online suggests that this seems to be a limitation of ExtJS 4.2 MVC. Is this true? Below is what I have tried so far.

First, the custom view definition (a basic container example):

Ext.define('My.custom.TestView', {
    extend: 'Ext.container.Container',
    alias: 'widget.myCustomTestView',
    html: 'Test',

    initComponent: function() {
        this.callParent(arguments);
    }
});

Second, the controller definition that's trying to load/instantiate the custom view:

Ext.define('My.custom.TestController', {
    extend: 'Ext.app.Controller',

    // Empty array because I want to
    // add views during run-time.
    views: [
    ],

    init: function () {
        // Attempt #1: This produces an error.
        this.getView('My.custom.TestView').create();

        // Attempt #2: This returns null.
        Ext.widget('myCustomTestView');

        // Attempt #3: This appears it might work, but it's ugly!
        Ext.create('My.custom.TestView');
        this.getView('My.custom.TestView').create();
    }
});

While #3 is the only one that does not give errors, it does not look right to me. Any suggestions/comments?

4

1 回答 1

1

Ext.create() 会导致同步加载,因此它实际上会检查您的视图的依赖 js 文件是否已加载,如果未加载,则它会同步加载 js 文件并停止执行你的代码,直到 js 被 Ext.Loader 加载和初始化。然后,您可以在该类上 getView().create(),因为您的视图类现在在 Ext 中可用。

对于另外两个:

  1. 会产生一个错误,因为它基于 My.custom.TestView 已经被加载,它不是,所以它无法在它加载的内容中找到类的定义以创建它的实例。

  2. 返回 null 很可能是因为 Ext.widget 的实现方式,以便保持对 Ext.widget() 的调用,该调用指定尚未加载的类的小部件名称,以免破坏您的应用程序。

1 和 2 都与 Ext.create() 不同,因为它们调用 Ext.Loader 来解析类文件并在没有发现它已经在内部加载到 Ext 时加载它。它们只是作用于已经加载并且在调用它们时在您的应用程序中可用的代码。

值得在这里http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Loader阅读 Exts 文档中的 Ext.Loader

于 2013-05-24T19:26:19.773 回答