boilerplatejs – 延迟加载样板模块

在样板中看起来模块是预加载的

(参考下面的代码)

return [
            require('./baseModule/module'),
            require('./sampleModule2/module'),
            require('./customerModule/module'),
            require('./orderSearchModule/module'),
            require('./orderListModule/module'),
            require('./mainMenuModule/module')
        ];

当涉及大规模Web应用程序(模块繁重的Web应用程序)时,对此有何影响.有没有办法在boilerplatejs中延迟加载模块?

最佳答案 Java脚本没有反射类型的机制来加载东西.任何需要加载的模块都必须在某处注册.这就是为什么模块(子上下文)加载如下:

appContext.loadChildContexts(moduleContexts);

(在src / application.js中)

但是你上面的代码是关于requirejs AMD模块的.这基本上是导入不同的JS脚本(每个脚本代表模块的代码).使用requireJS AMD,脚本(您的源代码)不是延迟加载的,而是预先加载的.这是有道理的,因为您的应用程序源代码应该在浏览器中可用于执行.另一方面,当您进行JS优化时,我们无论如何都会创建一个包含所有源代码的脚本.然后没有单独的脚本文件或延迟加载源代码的意义.

但延迟加载应该应用于行为(而不是加载代码).这就是UI组件(ViewTemplate)仅在’activate()’方法中创建的原因.这些仅在用户需要时创建.这是一种延迟的行为加载,因此应用程序渲染时间较短.

    this.activate = function(parent, params) {
        // if panel is not created, lets create it and initiate bindings
        if (!panel) {
            panel = new Boiler.ViewTemplate(parent, template, nls);
            ...
        }
        vm.initialize(params.name);
        panel.show();
    }
点赞