In-place editing with Rails, jquery and best_in_place gem

怎甘沉沦 提交于 2020-01-04 03:15:12

问题


I've got a task list app where tasks are displayed by category.

I have the categories listed in two places. I'm allowing the category name to be edited in-place using the best_in_place gem. I have that working fine. My issue is that since I have the category name in two places and I'm only editing one occurrence of it in-place, I need the other appearance of the name that was not edited to be updated with the new name after the form submits. What would be the best way to refresh/reload the affected category's name?

Thanks.


回答1:


You need to use a callback.

Using the description of the gem (https://github.com/bernat/best_in_place) i got this:

<%= best_in_place @user, :name, :data => {:user_name => @user.name}, :classes => 'custom_class'  %>

$('.custom_class').bind("ajax:success", function(){ alert('Name updated for '+$(this).data('userName')); });

So in your case you need to do something like that:

$("#other_ocurrence_id").html($(this).data('userName'));



回答2:


If you have this code (haml)

= best_in_place @user, :name, :classes => 'name-edit'

You do need a callback. However, in the context of the callback handler, 'this' represents the element the call was made from, so you could just do

$('.name-edit').on 'ajax:success', (event, data, status, xhr) ->
  $('.some-other-widget').html this.innerHTML

and get the new value to the other widget that way. The event in the call back handler also has currentTarget as a property, which is the widget that started the ajax request. However, I don't think that's needed.



来源:https://stackoverflow.com/questions/15469050/in-place-editing-with-rails-jquery-and-best-in-place-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!