3

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.

Jack
  • 1,125
  • 1
  • 13
  • 29

2 Answers2

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'));
Bernardo Mendes
  • 1,220
  • 9
  • 15
  • This won't work. The :data binding in best in place will store the original value the property referenced. You will have to do some parsing of the dom to get the new value. See my answer. – tehfoo May 02 '13 at 21:54
1

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.

tehfoo
  • 328
  • 2
  • 7