TLDR :CoffeeScript 및 Ruby on Rail의 자산 파이프 라인과 우려를 분리하는 좋은 방법은 무엇입니까?
RoR에서 여러 CoffeeScript 파일을 하나의 JS 파일로 결합하는 작업은 모두 동일한 익명 기능 블록에서 수행 할 수 있습니까?
긴 버전 :
은 내가 RoR에 웹 응용 프로그램의 일부를로드 할 몇 CS 파일이 있습니다. 궁금한 점은 : CoffeeScripts 및 Ruby on Rail 3.1의 자산 파이프 라인과 우려를 분리하는 좋은 방법은 무엇입니까?
main.js.coffee
window.MyApp = {} # to escape the CoffeeScript anonymous function block
# (I like the anonymous function block because it protects my other
MY_GLOBAL_SETTING = "world!"
$.click "#my_button" myApp.sayHello
# (I could use something like goog.bind here instead of using myApp. Any suggestions? Fat arrow?)
hello.js.coffee
MyApp.sayHello = sayHello() ->
doComplicatedStuff()
alert("Hello #{ MY_GLOBAL_SETTING }")
complicated.js.coffee :
의이 예제 코드로 다음을 사용하자
doComplicatedStuff =() ->
# some really complicated algorithm, for example
true
내 자산 디렉토리는 다음과 같이 구성했다 : 나는 빌드 프로세스의 일부로 커피 스크립트와 함께 파일을 컴파일하는 데 사용
assets/
application.js
application/
# javascript that gets used with the main application
secondary_page.js
secondary_page/
complicated.js.coffee
hello.js.coffee
main.js.coffee
secondary.js
//= require secondary_page/main.js.coffee
//= require secondary_page/complicated.js.coffee
//= require secondary_page/hello.js.coffee
, 이제 자산 파이프 라인을 대신 사용하고 싶습니다. 나는 RoR 3.1 kool-aid를 마시고있다! 하하, 진지하게, 자산 파이프 라인은 멋져 보입니다.
내가 겪고있어 문제는 secondary.js는 다음과 같은 것이있다 :이 전체 코드 사이에 공유되는 로컬 변수를 방지
(function() {
// main.js
).call(this);
(function() {
// complicated.js
).call(this);
(function() {
// hello.js
).call(this);
. MY_GLOBAL_SETTING 및 doComplicatedStuff은 sayHay에 사용할 수 없습니다.
그래서 ... 어떻게해야합니까? 나는 내 자신의 커스텀 컴파일 단계를 다시 도입하지 않고 좋은 방법을 생각할 수 없다.