1

私はCoffeescriptが初めてで、構文に苦労しています。CSで次のように書く方法を教えてくれる人はいますか?

$("#getLocation").click(function() {
  $('#location-loading').show();
  navigator.geolocation.getCurrentPosition(applyLocation);
  return false;
});

function 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();
}

私は次のように動作すると思っていましたが、関数を呼び出して false を返すとエラーが発生します (そのため、リンクをたどりません)。

$('#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()
4

1 に答える 1

1

単純な関数呼び出し (チェーンされていない)の括弧を省略()し、下部の文字列をダブルクォータに入れて#{}構文を使用できるようにすることができますが、コードがすでにかなりコーヒーっぽく見えることを除いて;)

$('#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()
于 2012-05-21T19:52:54.570 に答える