如何将Backbone.js与Require.js(r.js)一起使用,但在优化后会产生2个文件?

TIM*_*MEX 8 html javascript requirejs backbone.js r.js

我已经按照基本教程(运行后生成一个文件r.js)

问题是,我的main.js文件最后是500KB.那太大了.我想把它分成两个文件.

我想将我的main.js文件优化为两个文件:

  1. 一个保存首页和用户个人资料页面的页面,因为它们访问最多
  2. 一个包含所有其他页面(订购,帐户设置,配置文件设置等)

大多数人会点击首页和用户个人资料页面,我希望首先快速加载(同时在第二个主文件中将其他页面加载到后台)

问题是,我不知道该怎么做.有这样的例子在线,但这些例子不使用Backbone.他们没有介绍如何处理路由器和app.js

我很困惑...因为我只有一个app.js,一个router.js......我怎么能将router.js分成两个文件?

在处理Backbone时,我不知道如何拆分我的项目.

下面是代码

HTML PAGE(单页应用程序的入口点)

<html>
<head>
    <script type="text/javascript" data-main='/media/js/main' src='/media/js/lib/requirejs/require-jquery.js'></script>
</head>
<body>
    Hello
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Main.js

require.config({
    paths:{
        jquery: 'lib/requirejs/require-jquery',
        jquery_ui:'lib/jquery-ui/jquery-ui-1.10.3.custom',
        underscore: 'lib/underscore/underscore-min',
        backbone:'lib/backbone/backbone-min',
        backbone_viewhelper:'lib/backbone/backbone.viewhelper',
        text: 'lib/requirejs/text',
        birthdaypicker: 'lib/birthdaypicker/bday-picker',
        //more paths
    },
    waitSeconds: 30,
    shim:{
        'underscore':{
            exports: '_'
        },
        'backbone':{
            deps:[ 'underscore', 'jquery'],
            exports: 'Backbone'
        },
        'backbone_viewhelper':{
            deps:['underscore','backbone']
        }
    }
});


require([
    'app',
    'json2',
    'jquery_ui',
    'backbone_viewhelper',
    'bootstrap_js',
    'bootstrap_select',
    'birthdaypicker',
    'accounting',
    'numbersonly',
    'main_alert',
    'string_tools',
    'plupload',
    //more things here
], function(App){
    App.initialize();
});
Run Code Online (Sandbox Code Playgroud)

App.js

define([
    'jquery',
    'underscore',
    'backbone',
    'router'
], function($, _, Backbone, Router){    
    var initialize = function(){
        Router.initialize();
    }
    return {
        initialize: initialize
    };

});
Run Code Online (Sandbox Code Playgroud)

Router.js

define([
    'jquery',
    'underscore',
    'backbone',
    'modules/index/view',
    'modules/home/view',
    'modules/listings_search/view',
    'modules/profile/view',
    //more modules
], function($, _, Backbone, indexView, homeView,searchView, profileView){
    var AppRouter = Backbone.Router.extend({
        initialize:function(){
            _.bindAll(this);
        },
        routes:{
            '':'index',
            'home': 'home',
            'register': 'register',
            'login': 'login',
            'listings(/start/:start)(/num/:num)': 'search',
            'listings/create': 'listingsCreate',
            'listings/:listing_id/edit': 'listingsEdit',
            'orders/listings/:listing_id/create': 'ordersCreate',
            'orders/buyer(/start/:start)(/num/:num)': 'ordersListBuyer',
            'orders/seller(/start/:start)(/num/:num)': 'ordersListSeller',
            'orders/:order_id': 'orders',
            'orders/:order_id/messages':'messages',
            '*actions': 'defaultAction'
            //more stuff
        },
        index:function(){
            app_router_view.show(indexView);
        },
        search:function(start, num){
            var options = {
                filters:{
                    start: start,
                    num: num
                }
            };
            app_router_view.show(searchView, options);
        },
        static:function(template){
            app_router_view.show(staticView, { static_view: { template: template }});
        },
        profile:function(){
            app_router_view.show(profileView);
        },
        passResetCode:function(code){
            app_router_view.show(passCodeView, {'code':code});
        },
        //more stuff
        home:function(){
            app_router_view.show(homeView);
        },
        defaultAction:function(actions){
            this.navigate('/', { trigger:true});
        }
    });
    var initialize = function(){
        var app_router = new AppRouter;
        Backbone.history.start({pushState:true, root: '/'});
        $(document).on('click', 'a:not([data-bypass])', function (evt) {
            var href = $(this).attr('href');
            if(href){
                var protocol = this.protocol + '//';
                if (href.slice(protocol.length) !== protocol && href != '#') {
                    evt.preventDefault();
                    app_router.navigate(href, { trigger: true});
                }
            }else{
            }
        });
    };
    return {
        initialize:initialize
    }
});
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我的整个应用程序main.js都以app.js,开始,最后进入router.js.

我怎么能分开这个?

Man*_*ani 3

根据您共享的代码,我创建了一个示例 Web 应用程序并将代码提交到git-hub中。

应用程序分为2个模块:

  • maincontains modules/index/viewmodules/profile/view
  • othercontains 'modules/order/viewmodules/search/view

当您请求modules/index/view或时modules/profile/viewmain.js如果尚未下载,则会下载。modules/order/view同样,当对或发出请求时modules/search/viewother.js如果尚未下载,则下载。请记住使用require.js v2.1.10或更大,因为它具有生成other.js.

您可以通过order, search, profile在 中定义为独立模块来进一步模块化它build.js,以便仅在需要时下载它们。

执行构建命令的输出:

media/js/main.js
----------------
media/js/lib/jquery/jquery-min.js
media/js/lib/underscore/underscore-min.js
media/js/lib/backbone/backbone-min.js
media/js/router.js
media/js/app.js
media/js/main.js
media/js/modules/index/model.js
media/js/modules/index/view.js
media/js/modules/profile/model.js
media/js/modules/profile/view.js

media/js/other.js
----------------
media/js/modules/order/model.js
media/js/modules/order/view.js
media/js/modules/search/model.js
media/js/modules/search/view.js
Run Code Online (Sandbox Code Playgroud)

执行流程如下: index.html => media/js/main[它具有index/view, profile/view, app.js所有依赖项]。默认情况下显示索引视图,因为它是为家乡路由配置的。

单击“配置文件”链接后,不会再下载更多文件,因为main.js已经下载了文件。单击“搜索/订购”链接时,other.js将下载。