结构化coffeescript代码? [英] Structuring coffeescript code?

查看:138
本文介绍了结构化coffeescript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3.1下,我试图找出如何将一些coffeescript类从我的控制器默认coffeescript文件( home.js.coffee )移动到另一个

Under Rails 3.1, I'm trying to find out how to move a few coffeescript classes away from my controller default coffeescript file (home.js.coffee) into another file, in order to structure the whole a little bit.

有人知道如何将coffeescript文件包含到另一个文件中吗?

Does anyone know how to "include" a coffeescript file into another?

推荐答案

您想要做的是导出功能。例如,如果您以

What you want to do is export functionality. For instance, if you start with

class Foo
  ...

class Bar extends Foo
  ...

并决定移动 Foo 到其自己的文件,该文件应该像

and you decide you move Foo to its own file, that file should look like

class Foo
  ...

window.Foo = Foo

(其中 window.Foo = Foo 使 Foo 全局)和 Bar 以Sprockets指令开始

(where window.Foo = Foo makes Foo a global), and Bar's file should start with the Sprockets directive

#= require Foo

(假设您已命名 Foo 的档案 Foo.js.coffee )。每个文件独立编译为JS,但Sprockets将确保 Foo 包含在 Bar 之前。

(assuming that you've named Foo's file Foo.js.coffee). Each file is compiled into JS independently, but Sprockets will ensure that Foo is included before Bar.

注意,作为一个快捷方式,你可以摆脱 window.Foo = Foo 行,而不是写

Note that, as a shortcut, you can get rid of the window.Foo = Foo line, and instead write

class window.Foo
  ...

或只是

class @Foo
  ...

定义 Foo c $ c> window 对象。

to define a class named Foo that's attached to the window object.

这篇关于结构化coffeescript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆