Michael Schwartz
Michael Schwartz

Reputation: 8425

Write Ajax function in Coffeescript

Fiddle - http://codepen.io/mikethedj4/pen/BNRdVp

Today I decided to learn Coffeescript and play around making some functions, handling events, etc.

However today I got an error that says, "reserved word 'function'", and haven't figured out how to solve it.

Original:

$(function () {
    function download_to_textbox(url, el) {
        $.get(url, null, function (data) {
            el.val(data);
        }, "text");
    }
    download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));
});

My translation:

(($) ->
  function download_to_textbox(url, el) {
    $.get(url, null, (data) ->
      el.val(data);
      }, "text");
  }
  download_to_textbox("http://code.jquery.com/jquery-latest.min.js", $("textarea"));

  $("textarea").click ->
  $(this).select();
) jQuery

Upvotes: 1

Views: 178

Answers (1)

Umbra
Umbra

Reputation: 88

$ ->
    download_to_textbox = (url, el) ->
        $.get url, null, ((data) ->
            el.val data
            return
        ), 'text'

    download_to_textbox 'http://code.jquery.com/jquery-latest.min.js', $ 'textarea'
    $("textarea").click ->
        $(this).select()

Upvotes: 1

Related Questions