为什么这个coffeescript没有“正确”编译?

时间:2012-07-18 18:34:00

标签: javascript coffeescript

我在Rails应用程序中有以下内容:

# projects.js.coffee
jQuery ->
  $('#project_title').typing ->
    start: (event, element) ->
      element.css 'background', '#fa0'

    stop: (event, element) ->
      element.css 'background', '#f00'
      alert 'send request to populate samples'

    delay: 400

但是,在编译的js文件中,我得到以下内容:

(function() {

  jQuery(function() {
    return $('#sprinkler_word').typing(function() {
      ({
        start: function(event, element) {
          return element.css('background', '#fa0');
        },
        stop: function(event, element) {
          return element.css('background', '#f00');
        }
      });
      alert('send request to populate sprinkle samples');
      return {
        delay: 400
      };
    });
  });

}).call(this);

为什么“警报”调用未包含在“停止”功能中?我认为我的语法不正确,但我看不到在哪里。感谢。

1 个答案:

答案 0 :(得分:2)

将代码粘贴到“Try Coffeescript”小部件中,使用您期望的代码,即停止函数内的警报:

jQuery(function() {
  return $('#project_title').typing(function() {
    return {
      start: function(event, element) {
        return element.css('background', '#fa0');
      },
      stop: function(event, element) {
        element.css('background', '#f00');
        return alert('send request to populate samples');
      },
      delay: 400
    };
  });
});

因此,我猜测标签和空格会弄乱缩进。