Chassis icon indicating copy to clipboard operation
Chassis copied to clipboard

模块化按需加载

Open xspider opened this issue 12 years ago • 0 comments

<<返回目录

按需加载异步加载是不同的,按需加载是指:当需要某些SubView时,该SubView才及时创建并可用。

按需加载需要基于异步加载,并使用一些简单的方法,如示例中helloRocket里的代码:

(function($){

Chassis.PageView.sayhello = Chassis.PageView.extend({

    el: '#sayhello_page'

    ,init: function(options){
        var me = this,opt;

        opt = $.extend({}, options);

        me.prepend('say_header',opt);
        me.setup('sayhello_content',opt);

        // render异步子模块,也可以这样写:me.renderAsyncSubView(['say_header','sayhello_content']);
        me.renderAsyncSubView();
    }
});

})(Zepto);

代码中所示me.prepend('say_header',opt);me.setup('sayhello_content',opt);不会直接render到页面,只有在显示的调用renderAsyncSubView方法后才会生效。

如果renderAsyncSubView参数为空,则render当前PageView下所有异步的模块。支持指定render特定的异步模块,参数为renderAsyncSubView([asyncModule,...])

按需加载按需渲染可能更恰当一些,因为它并不是等到需要渲染时才异步加载再渲染的,而是提前异步加载,这样有助于减少这类模块下载等待的时间。

<<返回目录

xspider avatar Jul 23 '13 05:07 xspider