我如何用CoffeeScript写这个?

时间:2022-12-29 21:15:49

I'm having trouble with CoffeeScript in my first Rails application. I'm using the waitForImages jQuery plugin, which is saved in a separate file called waitforimages.jquery.js. Rails automatically created home.js.coffee, in which I'd like to include the following jQuery snippet:

我在第一个Rails应用程序中遇到了CoffeeScript问题。我正在使用waitForImages jQuery插件,该插件保存在一个名为waitforimages.jquery.js的单独文件中。 Rails自动创建了home.js.coffee,我想在其中包含以下jQuery代码段:

$('#fullbleed').waitForImages(function() {
    $(this).fadeIn(3000);
});

But how would I write this using CoffeeScript notation?

但是我怎么用CoffeeScript表示法写这个呢?

UPDATE

UPDATE

Things are working great now, so I thought I would post my final code. One of the issues was that I wasn't loading the waitForImages plugin before home.js.coffee.

现在情况很好,所以我想我会发布我的最终代码。其中一个问题是我没有在home.js.coffee之前加载waitForImages插件。

CoffeeScript:

CoffeeScript的:

$(document).ready -> $('#fullbleed').waitForImages -> $(@).fadeIn 3000

HTML:

HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Title</title>
  <script src="/assets/jquery.js?body=1" type="text/javascript"></script>
  <script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
  <script src="/assets/jquery.waitforimages.js?body=1" type="text/javascript"></script>
  <script src="/assets/home.js?body=1" type="text/javascript"></script>
</head>
<body>
...
</body>
</html>

3 个解决方案

#1


8  

Use -> ... instead of function(){ ... }. Optionally, you can also swap this with @.

使用 - > ...而不是function(){...}。或者,你也可以用@交换它。

$('#fullbleed').waitForImages ->
    $(@).fadeIn(3000)

If you really like to save characters, then you can also omit the last two parentheses, and get:

如果你真的想保存字符,那么你也可以省略最后两个括号,并得到:

$('#fullbleed').waitForImages -> $(@).fadeIn 3000

#2


3  

According to the very useful http://js2coffee.org:

根据非常有用的http://js2coffee.org:

$("#fullbleed").waitForImages ->
  $(this).fadeIn 3000

#3


2  

$('#fullbleed').waitForImages ->
  $(@).fadeIn 3000

or even:

甚至:

$('#fullbleed').waitForImages ->$(@).fadeIn 3000

#1


8  

Use -> ... instead of function(){ ... }. Optionally, you can also swap this with @.

使用 - > ...而不是function(){...}。或者,你也可以用@交换它。

$('#fullbleed').waitForImages ->
    $(@).fadeIn(3000)

If you really like to save characters, then you can also omit the last two parentheses, and get:

如果你真的想保存字符,那么你也可以省略最后两个括号,并得到:

$('#fullbleed').waitForImages -> $(@).fadeIn 3000

#2


3  

According to the very useful http://js2coffee.org:

根据非常有用的http://js2coffee.org:

$("#fullbleed").waitForImages ->
  $(this).fadeIn 3000

#3


2  

$('#fullbleed').waitForImages ->
  $(@).fadeIn 3000

or even:

甚至:

$('#fullbleed').waitForImages ->$(@).fadeIn 3000