Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Last active August 29, 2015 14:13
Show Gist options
  • Save ntijoh-daniel-berg/446c4fc0ab2d04731eac to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/446c4fc0ab2d04731eac to your computer and use it in GitHub Desktop.
$ ->
console.log('ready')
getSuggestions()
$('body').on 'submit', '.remover', ->
removeSuggestion($(this), event)
event.preventDefault()
removeSuggestion = (form, event) ->
$.ajax
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: ->
form.parent().remove()
getSuggestions = ->
$.ajax
url: '/suggestions.json',
dataType: 'json',
error: (jqXHR, textStatus, errorThrown) ->
$('body').append "AJAX ERROR: #{textStatus}, #{errorThrown}",
success: renderSuggestions
renderSuggestions = (suggestions) ->
output = '<ul>'
for suggestion in suggestions
output += '<li>'
output += " <span><a href='/suggestion/#{suggestion.id}'>#{suggestion.name}</a></span>"
output += " <form class='remover' action='/suggestion/#{suggestion.id}/remove' method='post'>"
output += ' <button type="submit">X</button>'
output += ' </form>'
output += '</li>'
output += '</ul>'
$('body').append(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment