2011-12-23 136 views
1

我试图在检查出昨天的css之后与CoffeeScript达成协议,我对此印象非常深刻。如何在CoffeeScript中编写此代码?

我比raw Javascript更像是一个jQuery wiz,所以我觉得它有点令人困惑,但同时我觉得考虑一下CoffeeScript是好的,因为它可以帮助我通过分析输出来获得更好的理解。

var Raw = (function($) { 

    $(function() { 
     Raw.initialize(); 
    }); 

    return { 
     _current: '', 
     initialize: function() { 
      this.initGlobal(); 
      if(this.is('index')) { 
       this.initIndex(); 
      } 
      else if(this.is('single')) { 
       this.initSingle(); 
      } 
     }, 
     initGlobal: function() { 
      atom_twitter(); 
      atom_loading(); 
      ratings(); 
     }, 
     initIndex: function() { 
      atom_scroll(); 
     }, 
     initSingle: function() { 
      atom_download(); 
     }, 
     is: function(page) { 
      if(this._current == '') { 
       this._current = $('body').attr('id'); 
      } 
      return this._current == page; 
     } 

    }; 

})(jQuery); 

任何想法从哪里开始?

到目前为止,我有这样的:

Raw = (($) -> 
    console.log 'hello world' 
)(jQuery); 

,输出:

(function() { 
    var raw; 

    raw = (function($) { 
    return console.log('hello world'); 
    })(jQuery); 

}).call(this); 
+0

嘛js2cofee转换器,我不认为代码将被_identical_,但功能不过,这绝对可以实现。 – omninonsense 2011-12-23 20:16:05

+2

我建议将此问题迁移到[Code Review](http://codereview.stackexchange.com/)。 – 2011-12-23 20:20:01

+0

@TrevorBurnham是某种类型的管理员能够做到这一点? – daryl 2011-12-23 20:53:07

回答

2

以下是我会做:

raw = do ($ = jQuery) -> 
    $ raw.initialize 
    { 
    _current: '' 
    initialize: -> 
     @initGlobal() 
     if @is 'index' 
     @initIndex() 
     else if @is 'single' 
     @initSingle() 
    # and the rest... 
    } 

注意,有可能是你试图将代码移植的一个问题:传递给$作用进行运行时的DOM已准备就绪,或者立即如果DOM已经准备就绪。因此,如果此代码运行时DOM已准备就绪,则将在调用raw之前调用raw.initialize,这会导致错误。

1

随着你的结构代码的方式,它是非常逐行转换线。我没有为$转换而烦恼,但如果你想要的话,你可以在do行后面加上$ = jQuery

Raw = null 
do ($ = jQuery) -> 

    Raw = 
    _current: '' 
    initialize: -> 
     @initGlobal() 
     if @is 'index' 
     @initIndex() 
     else if @is 'single' 
     @initSingle() 

    initGlobal: -> 
     atom_twitter() 
     atom_loading() 
     ratings() 
    initIndex: -> 
     atom_scroll() 
    initSingle: -> 
     atom_download() 

    is: (page) -> 
     @_current = $('body').attr('id') if @_current == '' 
     @_current == page 

    $ -> Raw.initialize() 
+4

从CoffeeScript 1.2.0开始,你可以写'do($ = jQuery) - >'来产生'(function($){...})(jQuery)'。 – 2011-12-23 22:22:06

+0

很高兴知道!这非常方便。 – loganfsmyth 2011-12-23 22:32:46

+0

好Trevor。 – daryl 2011-12-23 22:40:15

1

你也可以使用

Raw = (($) -> 
    $ -> 
    Raw.initialize() 

    _current: "" 
    initialize: -> 
    @initGlobal() 
    if @is("index") 
     @initIndex() 
    else @initSingle() if @is("single") 

    initGlobal: -> 
    atom_twitter() 
    atom_loading() 
    ratings() 

    initIndex: -> 
    atom_scroll() 

    initSingle: -> 
    atom_download() 

    is: (page) -> 
    @_current = $("body").attr("id") if @_current is "" 
    @_current is page 
)(jQuery) 

,如果你想你的JS转换为CoffeeScript的,请在http://js2coffee.org/