简体   繁体   English

ExtJS4 MVC延迟加载

[英]ExtJS4 MVC Lazy Loading

Help! 救命! I have an ExtJS 4.2 MVC application that will contain multitudes of various Views. 我有一个ExtJS 4.2 MVC应用程序,其中包含大量的各种视图。 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. 过去(在MVC之前),我使用Ext.create()动态加载我的.js类文件。 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. 但是,我试图使用MVC设计模式,并且我在网上阅读的所有内容都表明这似乎是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. 虽然#3是唯一不出错的代码,但对我来说却并不正确。 Any suggestions/comments? 有什么建议/意见吗?

Ext.create() causes a synchronous load, so what happens is it actually checks to see if the dependent js file for your view has been loaded, if it isn't then it does a synchronous load of the js file and stops execution of your code until the js is loaded and initialized by Ext.Loader. Ext.create()会导致同步加载,所以实际发生的是检查您视图的相关js文件是否已加载,如果未加载,则它会同步加载js文件并停止执行您的代码,直到由Ext.Loader加载并初始化js。 You can then getView().create() on the class because your view class is now available within Ext. 然后,您可以在该类上进行getView()。create(),因为您的视图类现在在Ext中可用。

For the other two: 对于其他两个:

  1. Would produce an error because its predicated on My.custom.TestView already being loaded, which it isn't, so it can't find the definition of the class in what's it has loaded in order to create an instance of it. 会产生一个错误,因为它基于My.custom.TestView的谓词已经被加载了,但尚未加载,因此它无法在加载的类中找到该类的定义以创建它的实例。

  2. Returns null most likely because of the way that Ext.widget is implemented, in order to keep a call to Ext.widget() which specifies a widget name for a class that hasn't been loaded yet from breaking your app. 为了保留对Ext.widget()的调用,该调用最有可能由于实现Ext.widget的方式而返回null,该调用为断开应用程序尚未加载的类指定窗口小部件名称。

Both 1 and 2 are different from Ext.create() in that they don't call Ext.Loader to resolve the class file and load it if it isn't found to be already loaded into Ext internally. 1和2与Ext.create()的不同之处在于,它们调用Ext.Loader来解析类文件并在未发现内部已将其加载到Ext时加载它。 They simply act on code that has already been loaded and is available in your application at the time they are called. 它们只是对已经加载的代码起作用,并且在调用它们时在您的应用程序中可用。

It's worth doing a bit of reading up on Ext.Loader in Exts docs here http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Loader 值得在这里的Exts docs中对Ext.Loader进行一些阅读, 网址为http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM