2012-06-25 37 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以上评论建议它。