2012-05-21 50 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