Skip to content

Instantly share code, notes, and snippets.

@radiokills
Created September 11, 2013 11:33
Show Gist options
  • Save radiokills/6522369 to your computer and use it in GitHub Desktop.
Save radiokills/6522369 to your computer and use it in GitHub Desktop.
modal-form
$.fn.modal_form = (element)->
modal_form = new ModalForm
$(document).delegate('form[data-modal-result=true]', 'ajax:success', (data, xhr, status) ->
modal_form.form_result(data,xhr,status)
)
$(document).delegate('form[data-modal-result=true]', 'ajax:error', (data, xhr, status) ->
modal_form.form_result(data, xhr, status)
)
$(document).delegate('[data-use-modal=true]', 'click', (event)->
event.preventDefault() if event.preventDefault?
modal_form.make_ajax_call(event)
)
modal_form
class ModalForm
default_options:
modal_selector: '#multi_purpose_modal'
content_selector: '.content'
constructor: (options) ->
console.log('initializing modal form')
@options = $.extend(@default_options, options)
@modal = $(@options.modal_selector)
@modal_content = @modal.find @options.content_selector
make_ajax_call: (event) ->
console.log 'calling ajax'
$.ajax(
url: event.currentTarget.href,
data:{ _method: DS.app.modal_form.get_event_parameters(event).method}
type: DS.app.modal_form.get_event_parameters(event).verb
)
.success (xhr, status, data)->
DS.app.modal_form.fill_modal(xhr)
fill_modal: (content) ->
@modal_content.html(content)
@modal.foundation('reveal', 'open') if !@modal.hasClass('open')
get_event_parameters: (event) ->
verb = $(event.currentTarget).data('verb')
if verb && verb == 'delete'
{verb: 'POST', method: 'delete'}
else
{verb: 'GET', method: 'get'}
form_result: (data, xhr, status) ->
@modal_content.html(xhr)
@modal.foundation('reveal', 'open') if !@modal.hasClass('open')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment