2012-01-26 33 views
4

我有以下列方式与远程=>真实的按钮调用一个弹出式窗口(一个jquery弹出,而不是真实的):滑轨3-弹出内渲染上的AJAX调用局部

$modal = $('#modal') 
$modal_close = $modal.find('.close') 
$modal_container = $('#modal-container') 
$task_select_div = $('.activity_task_add') 

# Handle modal links with the data-remote attribute 
$('a[data-remote]').on 'ajax:success', (xhr, data, status) -> 
    $modal 
    .html(data) 
    .prepend($modal_close) 
    .css('top', $(window).scrollTop() + 150) 
    .show() 

#This is the callback that is not being executed. 
$('form[data-remote]').on 'ajax:success', (xhr, data, status) -> 
    alert(data) 
    $modal_container.hide() 
    $modal.hide() 
    $task_select_div.html(data) 

在弹出我有这种形式我打电话的提交按钮remote_tag和行动是在底部下面的代码另一种形式:

respond_to do |format| 
    if @task.save 
     format.html { redirect_to @task, notice: 'Task was successfully created.' } 
     format.json { render json: @task, status: :created, location: @task } 
     format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}} 
    else 
    format.html { render action: "new" } 
    format.json { render json: @task.errors, status: :unprocessable_entity } 
    end 
end 

它执行format.js和控制台说“渲染任务/ _tasks .html.erb(5.8ms)“,但ajax调用的回调不起作用。

$('form[data-remote]').on 'ajax:success', (xhr, data, status) -> 
    alert(data) 

我需要接收ajax:success事件才能隐藏弹出窗口。 有什么帮助吗?

回答

1

解决它。

更改我的respond_to | format |为此:

if request.xhr? 
    task_list = render_to_string :partial => 'tasks', :locals => {:tasks => current_user.department.tasks} 
    task_list = task_list.html_safe.gsub(/\n/, '').gsub(/\t/, '').gsub(/\r/, '') 
    render :json => {:html => task_list, :error => ''} 
else 
    respond_to do |format| 
    if @task.save 
      format.html { redirect_to @task, notice: 'Task was successfully created.' } 
      format.json { render json: @task, status: :created, location: @task } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @task.errors, status: :unprocessable_entity } 
    end 
    end 
end 

而且我的JavaScript此:

$('form[data-remote]').on 'ajax:success', (xhr, data, status) -> 
    $modal_container.hide() 
    $modal.hide() 
    $task_select_div.html(data.html) 

你认为这个解决方案是什么?有什么缺点?

+1

FYI'.gsub(/ [\ n \ t \ r] /,'')'或'.gsub(/ \ n | \ t | \ r /,'')'可以避免多重gsubs。 – ocodo

1

删除此行:

format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}} 

更新您的JS回调:

$('form[data-remote]').on 'ajax:success', (xhr, data, status) -> 
    alert("hello world") 
    $modal_container.hide() 
    $modal.hide() 
    $task_select_div.html(<%= escape_javascript(render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}) %>) 
+0

如何传递局部变量?这是一个标准的轨道3的方式? 不应该使用coffeescript文件吗? – Tony

+0

我从来没有尝试过传递局部变量,只是实例变量。你不能使用实例变量吗?是的,这是非常标准的,你不应该专门使用coffescript文件。 http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/更多信息 – michel

+0

可能是'format.js {:locals => {:tasks => current_user.department.tasks}'工作正常吗? – michel