2012-05-21 49 views
1

我是Coffeescript的新手,并在语法上挣扎。任何人都可以帮助我如何在CS中编写以下内容?我认为下面的工作,但我收到错误调用函数和返回false(所以我不遵循链接)。Coffeescript函数语法

$('#getLocation').click -> 
    $('#location-loading').show() 
    navigator.geolocation.getCurrentPosition(applyLocation) 
    false 

applyLocation = (location) -> 
    $('#LogLongitude').val(location.coords.longitude) 
    $('#LogLatitude').val(location.coords.latitude) 
    alert('Latitude:' + location.coords.latitude + ', Longitude: ' + location.coords.longitude + ', Accuracy: ' + location.coords.accuracy) 
    $('#location-loading').hide() 
+1

使用js2coffee.org获取更多帮助。 –

+0

事实证明,代码工作正常,但我忘了在文件开头加入“jQuery - >”,因为我使用的是jQuery,而且由于复制/粘贴问题,我有几个选项卡而不是空格。 – Ryan

回答

1

可以省略() parethesis简单的函数调用(不链接) 并把字符串中下部为双配额,以便能够使用#{}语法, 但不同之处在于你的代码做看起来已经很咖啡了;)

$('#getLocation').click -> 
    $('#location-loading').show() 
    navigator.geolocation.getCurrentPosition applyLocation 
    false 

applyLocation = (location) -> 
    coords = location.coords 
    $('#LogLongitude').val coords.longitude 
    $('#LogLatitude').val coords.latitude 
    alert "Latitude: #{coords.latitude}, 
     Longitude: #{coords.longitude}, 
     Accuracy: #{coords.accuracy}" 
    $('#location-loading').hide() 
+0

感谢您的提示和清理代码。你的代码看起来更干净。我已经更新了我的建议。谢谢。正如我在上面的评论中提到的,我的代码实际上在将“jQuery - >”添加到文件的开始之后起作用。 – Ryan