javascript 使用 CoffeeScript 运行 JQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6969544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running JQuery with CoffeeScript
提问by Sean W.
How can I properly use jQuery and CoffeeScript? All of the examples that I have seen thus far compile the CofeeScript at runtime in the browser; this is not ideal. Normally, I'd simply write in plain old JavaScript, but I think CoffeeScript can allow me do get more done with less code, once I know how to get started. I've worked with JQuery before, but I've not used CoffeeScript. I'm not sure where to get started? Should I place $(document).ready in my external CofeeScript/Javascript?
如何正确使用 jQuery 和 CoffeeScript?到目前为止,我看到的所有示例都是在浏览器中在运行时编译 CofeeScript;这并不理想。通常,我会简单地用普通的旧 JavaScript 编写,但我认为一旦我知道如何开始,CoffeeScript 可以让我用更少的代码完成更多的工作。我以前使用过 JQuery,但我没有使用过 CoffeeScript。我不知道从哪里开始?我应该将 $(document).ready 放在我的外部 CofeeScript/Javascript 中吗?
回答by jasalguero
Just have to put the jquery code after $ ->
只需要在后面加上 jquery 代码 $ ->
Hereis a small article about it, and if you are starting The Little Book on CoffeeScriptis quite useful, it's very clear and goes from scratch
这里有一篇关于它的小文章,如果你刚开始使用The Little Book on CoffeeScript非常有用,它非常清晰,而且从头开始
回答by Trevor Burnham
All of the examples that I have seen thus far compile the CoffeeScript at runtime in the browser; this is not ideal.
到目前为止,我看到的所有示例都是在浏览器中在运行时编译 CoffeeScript;这并不理想。
Agreed. You should look at projects like The Middlemanthat let you transparently compile your CoffeeScript to JavaScript on a local server for development, then bundle up the minified JS for deployment. (The Middleman also includes support for Haml and Sass, if you're into those, but you can just use HTML and CSS as well.)
同意。你应该看看像The Middleman这样的项目,它让你在本地服务器上透明地将 CoffeeScript 编译为 JavaScript 以进行开发,然后捆绑缩小的 JS 进行部署。(中间人还包括对 Haml 和 Sass 的支持,如果您喜欢这些,但您也可以只使用 HTML 和 CSS。)
The big advantage of The Middleman (or Rails, or any other web framework with CoffeeScript support) over just running coffee -cw
is that the latest version of your compiled CoffeeScript is served every time you refresh the page; you never have to worry about waiting for background compilation to finish.
The Middleman(或 Rails,或任何其他支持 CoffeeScript 的 Web 框架)比只运行的最大优势coffee -cw
是每次刷新页面时都会提供最新版本的已编译 CoffeeScript;您永远不必担心等待后台编译完成。