简体   繁体   English

Rails 3.2:如何将大型javascript文件拆分为许多“部分”?

[英]Rails 3.2: How do I split large javascript file into many “partials”?

Using the Rails Asset Pipeline, I was wondering how you can split a large javascript file into many different partials. 使用Rails Asset Pipeline,我想知道如何将大型javascript文件拆分为许多不同的部分。

For example: 例如:

YUI().use('app', function(Y) {
    //*=require sub/file_a.js
    //*=require sub/file_b.js
    //*=require sub/file_c.js
    //*=require sub/file_d.js
});
// this obviously is not the answer & does not work.

This would just be for the developers benefit of having a cleaner looking JS File rather than a huge app full of YUI views & models. 这只是为了让开发人员受益,因为它拥有看起来更整洁的JS File,而不是拥有完整YUI视图和模型的庞大应用。

I've already tried using ERB to include a file with no luck. 我已经尝试过使用ERB来包含一个没有运气的文件。 Render is not available in the asset pipeline, so that is a bust as well. 资产管道中不提供渲染功能,因此也很困难。

Any ideas? 有任何想法吗?

The end result would still be compiled by rake compile:assets:all -- so this would only be used in the development environment. 最终结果仍将通过rake compile:assets:all进行编译-因此只能在开发环境中使用。

charlysisto had the right idea. charlysisto有一个正确的主意。

The answer was to use Sprockets & YUI Modules as the views / models. 答案是使用Sprockets和YUI模块作为视图/模型。

//*=require_self
//*=require view/file_a.js
//*=require model/file_a.js
//*=require view/file_b.js
//*=require model/file_b.js
YUI().use('app','app-view-file-a','app-view-file-b','app-model-file-a','app-model-file-b', function(Y) {
    Y.FileApp = Y.Base.create('fileApp', Y.App, [], {
        views: {
            fileA: {type: 'FileAView'},
            fileB: {type: 'FileBView'}
        }
    }, {
        ATTRS: {
            root: {value: '/'}
        }
    });

    var app = new Y.FileApp({
        contentSelector: '#pjax-content',
        serverRouting: true,
        transitions: true,
        container: '#file-app',
        viewContainer: '#file-app-views'
    }).render().showContent('#pjax-content', {view: 'FileAView'});
});

In the views & model files, you would just create them as you would for a normal YUI3 Module: 在视图和模型文件中,只需像创建普通YUI3模块那样创建它们即可:

YUI.add('app-view-file-a', function(Y) {
  Y.namespace('FileAView');
  FileAView = Y.Base.create('fileAView', Y.View, [], {
      template: Y.Handlebars.compile(Y.one('#file-a-template').getHTML())
  });
  Y.FileAView = FileAView;
},'0.1.0',{requires:['node','handlebars'], skinnable:false});

In the beginning, I actually got the File.read to work within the JS. 一开始,我实际上使File.read在JS中工作。 But this method seemed very messy and didn't work very well in the development arena. 但是这种方法看起来很杂乱,在开发领域中效果不佳。 Rails never knew to recompile the "parent" js file because the timestamp didn't change. Rails从不知道要重新编译“父” js文件,因为时间戳没有变化。 If you are not using YUI, the File.read solution may work for you -- but I'd look for a different solution. 如果您不使用YUI,则File.read解决方案可能适用于您-但我会寻找其他解决方案。 Was pretty annoying have to enter a new line & delete every time I made a change to the child modules. 每当我对子模块进行更改时,必须输入一个新行并删除它就很烦人。

Depending on the content of your partials you could wrap the objects they continain in vars so you could then do : 根据部分的内容,您可以将它们continain的对象包装在vars中,然后可以执行以下操作:

//*=require sub/file_a.js
//*=require sub/file_b.js
//*=require sub/file_c.js
//*=require sub/file_d.js

YUI().use('app', function(Y) {file_a + file_b ...})

Not sure though it's a fit in your case, not knowing what's in the partials and what kind of argument YUI().use takes... 不知道这是否适合您的情况,不知道部分中有什么以及YUI()。use.use需要什么样的参数。

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

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