6

I'm using best_in_place to do in-page editing of a table of data in a ruby-on-rails app. The in-place editing works, but I have a corner case that fails. A pair of items in the row (device_name, generic_name) must be unique. If they are not unique, the server-side code passes back a set of names with a changed generic_name to make the pair unique. I use the following coffeescript to update the display.

jQuery ->
    $('.best_in_place[data-bip-object="full_dpoint"]').bind(
        "ajax:success", (event, d) ->▫
            return if ! d?
            data = JSON.parse(d)
            if ! data.dpoint?
                return
            else
                item_to_edit = "#best_in_place_full_dpoint_" + data.dpoint.dpoint_id + "_generic_name"
                $(item_to_edit).text(data.dpoint.generic_name)
            )

This code works (IE it properly updates the page with the server-supplied new generic name) , but if I then click back in the 'generic_name' field, (to go into edit mode), the default edit text changes back to what it was at the very beginning (page download time). I have experimented with setting many different page elements to the new generic name, including all of the following:

$(item_to_edit).attr('data-bip-original-content', data.dpoint.generic_name)
$(item_to_edit).attr('data-bip-value', data.dpoint.generic_name)
$(item_to_edit).attr('original-value', data.dpoint.generic_name)
$(item_to_edit).attr('bipValue', data.dpoint.generic_name)
$(item_to_edit).attr('bipvalue', data.dpoint.generic_name)

All to no avail. I have poked around in the dom trying to find where the original value might be stored, but haven't found anything other than these.

Any ideas?

TIA.

Leonard

Leonard
  • 13,269
  • 9
  • 45
  • 72
  • quick question...after the value gets changed in the page..what do you get if in case you do a page reload? Does it show the changed value that your javascript code is making or it reverts back to old field entered by best in place? – uday Feb 04 '16 at 14:18
  • After reload, it correctly shows the changed value. I should also add that I don't need to reload the page to get the changed value, if I just click outside the 'edit' box in also goes back to the correct (changed) value. – Leonard Feb 04 '16 at 18:41

1 Answers1

0

Try it

javascript:

$(document).ready(function () {
    /* Activating Best In Place */
    jQuery(".best_in_place").best_in_place();
    var old_value;
    $(document).on('change', '.not_abort', function (e) {
        console.log(e.target.value);
        old_value = e.target.value;
    });
    $(document).on('ajax:error', '.not_abort', function (e) {
        console.log(e);
        e.target.innerHTML = old_value;
        $(e.target).data('bipOriginalContent', old_value);
    });

})

view:

best_in_place @device_name, :email, class: 'not_abort' 
Marat Amerov
  • 406
  • 2
  • 9