2013-05-14 60 views
0

我有一個刪除按鈕與遠程:真正的選項設置:重新運行的JavaScript成功的Ajax調用

# _categories.html.erb 
<%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %> 

我破壞方法使用JSON:

# categories_controller.rb 
    def destroy 
    @category = Admin::Category.find(params[:id]) 
    @category.destroy 
    save_to_history("The category \"#{@category.name}\" has been destroyed", current_user.id) 

    respond_to do |format| 
     # You can't retrieve the @categories before attempting to delete one. 
     @categories = Admin::Category.all 

     format.json 
    end 
    end 

和我destroy.json.erb文件看起來像:

#destroy.json.erb 
<% self.formats = ["html"] %> 
{ 
    "html":"<%= raw escape_javascript(render :partial => 'categories', :content_type => 'text/html') %>" 
} 

現在我的問題是,我有這個JavaScript在頁面加載運行,並刪除初始分類可以作爲intende d ...直到數據改變。每當用戶通過添加新類別或刪除類別來更改數據時,我都需要使用下面的JavaScript再次運行。怎麼樣?請善待您的幫助,我根本不知道JavaScript!哈哈

<script type='text/javascript'> 

    $(function(){ 
     /* delete category */ 
     $('a[data-remote]').on('ajax:success', function(event, data, status, xhr){ 
     $("#dashboard_categories").html(data.html); 
     }); 
    }); 

    </script> 

FULL index.html.erb:

# index.html.erb 
<% title "Categories" %> 

<%= form_for @category, remote: true do |f| %> 
    <% if @category.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2> 

     <ul> 
     <% @category.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <table> 
    <tr> 
     <td><%= f.text_field :name, placeholder: 'Category Name' %></td> 
     <td><%= f.text_field :color, placeholder: 'Colour' %></td> 
     <td><%= f.submit %></td> 
    </tr> 
    </table> 
    <br /> 
<% end %> 


<div id="dashboard_categories"> 
    <%= render partial: 'categories' %> 
</div> 


<% content_for :javascript do %> 
    <script type='text/javascript'> 

    $(function(){ 

     /* new category */ 
     $('#new_admin_category').on('ajax:success', function(event, data, status, xhr){ 
     $("#dashboard_categories").html(data.html); 
     }); 

     /* delete category */ 
     $('a[data-remote]').on('ajax:success', function(event, data, status, xhr){ 
     $("#dashboard_categories").html(data.html); 
     }); 
    }); 

    </script> 
<% end %> 
+0

即使有更新你會收到unexisted類別的情況下,所以它也保護自己在服務器端是個好主意。 – fotanus 2013-05-14 11:33:13

+0

我同意@fotanus,有沒有簡單的方法來擴展我接受的答案? – 2013-05-14 11:51:13

+0

現在不要編輯它,因爲會使現有答案無效。編碼時請考慮這一點。 – fotanus 2013-05-14 12:03:43

回答

1

我不知道如果我有理解你100%,但看起來要添加AJAX成功後的事件?

$(function() { 
    function InitializeEvents() { 
     /* new category */ 
     $('#new_admin_category').on('ajax:success', function (event, data, status, xhr) { 
      $("#dashboard_categories").html(data.html); 
      InitializeEvents(); 
     }); 

     /* delete category */ 
     $('a[data-remote]').on('ajax:success', function (event, data, status, xhr) { 
      $("#dashboard_categories").html(data.html); 
      InitializeEvents(); 
     }); 
    } 
    InitializeEvents(); 
}); 

希望它能幫助:)

+1

正是我需要的,謝謝! :) – 2013-05-14 11:50:18

+0

歡迎:) – 2013-05-14 11:55:55