【问题标题】:how to use requirejs for non single page web apps?如何将 requirejs 用于非单页 Web 应用程序?
【发布时间】:2014-01-06 10:13:11
【问题描述】:

Backbone 是组织我们的 javascript 代码的必要条件,我一直在非单页应用程序中使用它,但直到现在我一直按如下方式构建我的 codeigniter 应用程序:

   /
   /profile
   /activity
   /....

其中一些页面共享 javascript 函数、主干视图、主干模型等

当我拥有跨多个页面共享的 javascript 功能时,我正在做的是将它包含在 main.js 中

/profile
    ...jquery.js
    ...backbone.js
    ...other stuff...

...main.js   <--- shared scripts
...page.js   <--- specific page script (e.g profile.js) 

问题是当我已经有一个模型,我想在另一个 javascript 特定页面(但不是在所有其他页面)中使用时,我必须将该段代码复制到 other-page.js

这很烦人,一个更具可扩展性和模块化的解决方案会很棒。 但我不使用 require.js,所以我在这里寻求可能有助于组织我的代码的模式。

非常感谢。

【问题讨论】:

    标签: codeigniter backbone.js requirejs


    【解决方案1】:

    您可以使用 requirejs 模块并为多个页面创建多个模块,并为您的常用内容创建一个通用模块:

    • 每个页面都使用通用模块和页面特定模块的组合。
    • 所有页面共享相同的 requirejs 配置。
    • 优化构建后,公共项应位于共享公共层中,页面特定模块应位于页面特定层中。

    这个配置可以是(build.js):

    {
      // ther 'regular' configuration,
        modules: [{
            name: '../common',         
            include: ['jquery', 'app/lib', 'app/controller/Base','app/model/Base']
          },{
            name: '../page1',
            include: ['app/main1'],
            exclude: ['../common']
        },{
            name: '../page2',
            include: ['app/main2'],
            exclude: ['../common']
        }]
    }
    

    查看https://github.com/requirejs/example-multipage了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多