2012-06-25 70 views
7

我在CoffeeScript中嘗試這樣的:的CoffeeScript和jQuery鏈接

$(element).mousedown(aFunction).mouseup(anotherFunction); 

我試圖找出一種方法,使使用縮進,使像下面這樣將返回的內容是關於:

$ element 
    .mousedown aFunction 
    .mouseup anotherFunction 

但無濟於事,是否有任何鏈接咖啡標記的建議?

+2

可能重複的[Coffeescript - 方法鏈接函數參數](http://stackoverflow.com/questions/5144191/coffeescript-method-chaining-with-function-arguments) –

回答

12

我敢肯定,你不希望使用括號,但是......

$("#element") 
    .mousedown(aFunction) 
    .mouseup(anotherFunction) 

編譯成

$("#element").mousedown(aFunction).mouseup(anotherFunction); 
+0

我認爲這是正確的解決方案在雖然這個問題[有](https://github.com/jashkenas/coffee-script/issues/1495)[was](https://github.com/jashkenas/coffee-script/issues/1407)[ (https://github.com/jashkenas/coffee-script/issues/1889)[many](https://github.com/jashkenas/coffee-script/issues/944)[times](https:/ /github.com/jashkenas/coffee-script/issues/2114)在[CoffeeScript的GitHub](https://github.com/jashkenas/coffee-script),所以對於這種嵌套的一些支持可能會在未來出現= d – epidemian

1

對於所有其他快速讀者在那裏,這裏的更新答案由a paid nerd給出here

req = $.get('foo.html') 
    .success (response) -> 
    do_something() 
    .error (response) -> 
    do_something() 

...編譯成:

var req; 
req = $.get('foo.html').success(function(response) { 
    return do_something(); 
}).error(function(response) { 
    return do_something(); 
}); 

貌似mus is too short以上評論建議它。