Sha256: 812cb0ed2402c941368c217d143efa625fe6378270ca620eee2fe0a2700a0e04

Contents?: true

Size: 1.31 KB

Versions: 50

Compression:

Stored size: 1.31 KB

Contents

# Method
#
# Allow links to be followed with an alternate method
# using `data-method`.
#
# To fake this, a hidden form element is created on the fly
# and is submitted.
#
# ### Markup
#
# `<a>`
#
# ``` definition-table
# Attribute - Description
#
# `data-method`
#   Method for following `href`. Can be `"post"`, `"put"`, or
#   `"delete"`. `"get"` is ignored since its the default behavior for
#   links.
# ```
#
#     <a href="/posts/1" data-method="delete">Delete</a>

$(document).on 'click', 'a[data-method]', (event) ->
  element = $(this)

  # Don't handle remote requests
  return if element.is 'a[data-remote]'

  method = element.attr('data-method').toLowerCase()

  # Skip GET requests
  return if method is 'get'

  # Create a dummy form
  form = document.createElement 'form'
  form.method = 'POST'
  form.action = element.attr 'href'
  form.style.display = 'none'

  # Set `_method` to simulate other methods like PUT and DELETE.
  if method isnt 'post'
    input = document.createElement 'input'
    input.setAttribute 'type', 'hidden'
    input.setAttribute 'name', '_method'
    input.setAttribute 'value', method
    form.appendChild input

  # Add it to the document and fire it off
  document.body.appendChild form
  $(form).submit()

  # Prevent default action so we don't follow the link
  event.preventDefault()
  false

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
wrgem-0.0.55 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.54 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.53 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.52 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.51 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.50 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.49 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.48 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.47 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.46 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.45 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.44 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.43 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.42 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.41 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.40 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.39 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.38 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.37 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee
wrgem-0.0.35 lib/generators/wrstart/template/vendor/assets/components/rails-behaviors/method.coffee