Tuesday, 10 September 2013

Can't get modal to disappear after successfully submitting form.

Can't get modal to disappear after successfully submitting form.

I'm trying to debug this coffeescript/js problem, but can't figure out
where my problem is.
I have the following CoffeeScript to create a modal that contains a form.
I can submit the form, which creates the record, but doesn't dismiss.
$(document).ready ->
$("#create_workout").click (e) ->
url = $(@).attr("href")
dialog_form = $("<div id=\"dialog-form\" title=\"Add New
Workout\">Loading form...</div>").dialog(
modal: true
autoOpen: false
closeText: "x"
closeOnEscape: true
hide: "fade"
show: "fade"
width: 550
dialogClass: "admin_forms"
open: ->
$(@).load url + " #content"
close: ->
$("#dialog-form").remove()
)
dialog_form.dialog "open"
e.preventDefault()
I also have a file in the workouts folder called create.js.erb that logs
'Error' if there is an error, and logs 'Created' and appends the new
workout to a table if the record is created. The code looks like this:
<% if @workout.errors.any? %>
console.log('Error');
$('#dialog-form').html('<%= escape_javascript(render('form')) %>');
<% else %>
console.log('Created');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
$('table').append('<%= escape_javascript(render(@workout)) %>');
$('table').effect('highlight');
<% end %>
When I create a record with the modal form I can see the following in the
console:
XHR finished loading: "http://localhost:3000/workouts/63".
jquery.js?body=1:8707
send jquery.js?body=1:8707
jQuery.extend.ajax jquery.js?body=1:8137
$.rails.rails.ajax jquery_ujs.js?body=1:74
$.rails.rails.handleRemote jquery_ujs.js?body=1:150
(anonymous function) jquery_ujs.js?body=1:357
jQuery.event.dispatch jquery.js?body=1:5096
elemData.handle
If I try to run the above in the console I get the following syntax error:
SyntaxError: Unexpected identifier
get stack: function () { [native code] }
message: "Unexpected identifier"
set stack: function () { [native code] }
__proto__: Error
I'm new to javascript, and haven't been able to figure out how this is
helpful at all. any ideas?
Thanks!

No comments:

Post a Comment