简体   繁体   English

使用CoffeeScript运行JQuery

[英]Running JQuery with CoffeeScript

How can I properly use jQuery and CoffeeScript? 我怎样才能正确使用jQuery和CoffeeScript? All of the examples that I have seen thus far compile the CofeeScript at runtime in the browser; 到目前为止,我所看到的所有示例都是在运行时在浏览器中编译CofeeScript; 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. 通常情况下,我只是用简单的旧JavaScript编写,但我认为CoffeeScript可以让我用更少的代码完成更多工作,一旦我知道如何开始。 I've worked with JQuery before, but I've not used CoffeeScript. 我之前使用过JQuery,但我没有使用过CoffeeScript。 I'm not sure where to get started? 我不知道从哪里开始? Should I place $(document).ready in my external CofeeScript/Javascript? 我应该在我的外部CofeeScript / Javascript中放置$(document).ready吗?

Just have to put the jquery code after $ -> 只需要在$ ->之后放入jquery代码

Here is a small article about it, and if you are starting The Little Book on CoffeeScript is quite useful, it's very clear and goes from scratch 是一篇关于它的小文章,如果你开始使用CoffeeScript上的Little Book非常有用,它非常清晰并且从头开始

All of the examples that I have seen thus far compile the CoffeeScript at runtime in the browser; 到目前为止,我看到的所有示例都是在运行时在浏览器中编译CoffeeScript; this is not ideal. 这不太理想。

Agreed. 同意。 You should look at projects like The Middleman that let you transparently compile your CoffeeScript to JavaScript on a local server for development, then bundle up the minified JS for deployment. 您应该查看像The Middleman这样的项目,它们允许您在本地服务器上透明地将CoffeeScript编译为JavaScript以进行开发,然后捆绑缩小的JS以进行部署。 (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还包括对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; 只有运行coffee -cw ,The Middleman(或Rails,或任何其他支持CoffeeScript的Web框架)的巨大优势在于每次刷新页面时都会提供已编译的CoffeeScript的最新版本。 you never have to worry about waiting for background compilation to finish. 你永远不必担心等待后台编译完成。

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

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