In-place editing with Rails, jquery and best_in_pl

2019-07-04 05:13发布

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.

2条回答
Bombasti
2楼-- · 2019-07-04 05:49

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'));
查看更多
女痞
3楼-- · 2019-07-04 06:14

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.

查看更多
登录 后发表回答