Skip to content

Instantly share code, notes, and snippets.

@andy23512
Last active October 8, 2017 07:47
Show Gist options
  • Save andy23512/d7b5334335a1c9b00b617c374abd84ba to your computer and use it in GitHub Desktop.
Save andy23512/d7b5334335a1c9b00b617c374abd84ba to your computer and use it in GitHub Desktop.
modal-ajax-form: frequently used combination
window.$.fn.modal-ajax-form = (opt) ->
$modal = $ @
$form = $modal.find \form
ajax-form-opt = do
before-submit: ->
$modal.find \button .add-class \loading
$form.find \.field.error .remove-class \error .find \span .empty!
success: ->
$modal.find \button .remove-class \loading
if it.ok
$form.add-class \success
if opt.on-ok then opt.on-ok it
set-timeout (-> $modal.modal \hide), 500
else
for field, error of it.errors then $form.find "[name=#field]" .parents \.field .add-class \error .find 'label > span' .text " - #{error[0]}"
if opt.ajax-form
ajax-form-opt <<< opt.ajax-form
$form.find \select .change!
$modal.modal do
autofocus: false
on-approve: ->
$form.ajax-submit ajax-form-opt
return false
on-hidden: ->
$form.remove-class \success
$form.find \.field.error .remove-class \error .find \span .empty!
$form[0].reset!
$form.find \.ui.dropdown .dropdown 'restore defaults'
.modal \show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment