Search code examples
jqueryruby-on-railssimplemodal

Jquery SimpleModal close on save


I have a calendar where you click on a day to add an event through a simple modal popup form. I am using Eric Martin's jquery plugin and I cant seem to make the form close without stoping the ajax response.

Here is my code:

function simple_dialog_link(request, title) {
  $.get(request, function(data) { 
    $.modal("<div class='modal'><div class='bar' >"+title+"</div><div class='inside'>"+data+"</div></div>");

    $(".modal").draggable({ handle: ".bar" }); 

    $("input.save").live("click", function(){
           $.modal.close();
    }); 
  });
};

This loads the form perfectly and does close the window but then redirects to my html path under my controller:

def update
  @log_entry = LogEntry.find(params[:id])
  @athlete = User.find_by_id(@log_entry.athlete_id)
  @log_entry.update_attributes(params[:log_entry])
  @date = @log_entry.date.to_s
  @cal_class = LogEntry.get_class_name(@date)
  @entries = LogEntry.cal_entry(@date, @athlete.id)
  respond_to do |format|
    format.html { redirect_to coaches_path }
    format.js
  end
end

What I have tried so far is to add a return: false under the modal close function, this closes the modal window but fails to update the calendar or database, thus cutting the form before it can submit to the controller. I need some function to close the window without interrupting my ajax functions. What is weird is the form submits fine and updates the calendar when I remove the $.modal.close(); function, just leaving the modal window open. I need this functionality but with the window closing.


Solution

  • Okay I figured it out, shows how much I know about all this! All I had to do was put the close function inside a ajax:complete callback.

    $(".edit_log_entry").bind('ajax:complete', function() {$.modal.close();});