2017-08-14 77 views
-3

我有一個非常巨大的index.js文件,如7000行,並希望將其拆分爲單獨的文件,不關心他們將由軌道組裝到一個文件,只需要將它們分開以便於編輯。紅寶石軌道拆分index.js分離文件

我有application.js清單文件,其中包括index.js和更多的JavaScript文件,但該index.js變得非常大,因爲我不是一個Ruby on Rails的人,所以你可以請給我一些有用的建議在上面?

index.js是:

「use strict」;

(function($) { 
//some setup string vars and arrays 

//some object definition 
//some object definition 
//some object definition 
//some object definition 

//some conditions to init those objects 
})(jQuery) 

所以

//一些對象定義

我想要移動到分隔的文件。

+0

你爲什麼要拆分爲獨立的文件?每個文件都是特定於某個頁面的JS? – styfle

+0

首先,爲了便於維護,不要在編輯時查看7000行js文件 –

回答

1

在你application.js文件,你可以撥打以下加載單個文件

//= require customjsfile1 
//= require customjsfile2 
... 

如果你只是想打電話給他們在您的ERB可以轉而做<%= javascript_include_tag "customjsfile1", "data-turbolinks-track" => true %>