2011-12-23 135 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/