Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i make a $.get request using coffeescript?

Tags:

How do I do the following in CoffeeScript?

  $( function() {     $('input#username').keyup( function() {       var username = $('input#username').val();       url = '/users/check_username/';       params = { username : username };       $.get(url, params, function(response){ markUsername(response); }, "json");     });   }) 
like image 730
Tum Avatar asked Jan 31 '11 10:01

Tum


People also ask

Is CoffeeScript still a thing?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).

How do I use CoffeeScript in HTML?

If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.


2 Answers

Here's another slightly condensed way to write it:

$ ->   $('input#username').keyup ->     username = $(this).val()     callback = (response) -> markerUsername response     $.get '/users/check_username/', {username}, callback, 'json' 

Note the lack of parens, and the shorthand "{username}" object literal.

like image 97
jashkenas Avatar answered Oct 21 '22 02:10

jashkenas


This is the best generic pattern I've come up with so far:

$.ajax '/yourUrlHere',   data :     key : 'value'   success  : (res, status, xhr) ->   error    : (xhr, status, err) ->   complete : (xhr, status) -> 

It compiles down to:

$.ajax('/yourUrlHere', {   data: {     key: 'value'   },   success: function(res, status, xhr) {},   error: function(xhr, status, err) {},   complete: function(xhr, status) {} }); 
like image 30
bennedich Avatar answered Oct 21 '22 03:10

bennedich