2012-01-26 29 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