bernat / best_in_place

A RESTful unobtrusive jQuery Inplace-Editor and a helper as a Rails Gem
http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/
1.2k stars 572 forks source link

:place_holder option not working #465

Open deemytch opened 9 years ago

deemytch commented 9 years ago

haml code like that:

=best_in_place @dish, :descr, url: "/dishes/#{@dish.id}", :place_holder => 'Нужно описание', value: @dish.descr, :as => :textarea

gets replaced in resulting html with:

<span id="best_in_place_dish_276_descr" class="best_in_place" data-bip-url="/dishes/276" data-bip-object="dish" data-bip-placeholder="Нужно описание" data-bip-attribute="descr" data-bip-type="textarea" data-id="276" data-field="descr"></span>

and if @dish.descr is nil - nothing was displayed. I see that the place_holder key was replaced with data-bip-placeholder attr, if that matter? I checked variants like :'place_holder' => , 'place_holder' =>, :'place-holder' etc, and even data: { bip: {placeholder: "text"}} but nothing works.

The same thing with other fields types.

Partial with fields to edit was loaded dinamically with ajax, if this does matter.

rails 4.2.0 best_in_place gem 3.0.3 ruby 2.2.1p85

stereodenis commented 9 years ago

@deemytch could you make a sample app with this bug?

deemytch commented 9 years ago

Unfortunately no. When I do pure new page outside of project it works as expected, that's why I decided to use that plugin in my project. But inside my project I get no placeholders at all. There's no errors in firefox console, all js code is included without errors in rails log. I spend yesterday full day to clean up all unneeded js code from all views, but without success. Have no more ideas how to debug that. Is there some points inside the lib, so I can check them?

stereodenis commented 9 years ago

@deemytch you can run internal gem app to test all behaviors by running rackup -p 3000 in gem folder

deemytch commented 9 years ago

Ok, I got it. This happens when I load the partial with ajax. What's confusing, is that all other functionality of best_in_place works as expected in such loaded view. Project: https://github.com/deemytch/best_in_place_test

stereodenis commented 9 years ago

@deemytch ok, I will check your repo

Miking commented 9 years ago

the same thing is happening to me and doesn't seem to be related to ajax.

hrangel commented 9 years ago

Could it be related to the initialization? Try wrapping its initializer in a document ready. Like that:

$(document).ready(function() {
    $('.best_in_place').best_in_place();
});
mvassilevsky commented 8 years ago

I'm also having this issue - was it ever resolved? In my case, the placeholder text doesn't load when I visit the page from a link, but if I refresh the page, it shows up.

acrogenesis commented 8 years ago

This is still an issue, I'm loading the content using js. <%= escape_javascript(render partial: 'info', locals: {persona: @persona}) %>

acrogenesis commented 8 years ago

Ok so below that line I just added $(".best_in_place").best_in_place(); and it worked. I was loading the script in document.ready but the document has been dynamically updated so I have to do it again. :+1:

jetian commented 8 years ago

I tried the solutions but I can't still get it to work. I'm using best_in_place inside https://github.com/antillas21/ajax-datatables-rails/

majedian21 commented 8 years ago

I'm having the same problem as @mvassilevsky - missing field only shows after a page refresh. I have the best_in_place initializer in a document ready in the application.js. Any updates on this issue?

nicbor commented 8 years ago

@majedian21 I had a similar problem. I was losing my placeholders when browsing around with turbolinks.

Like @acrogenesis said, I just added $(".best_in_place").best_in_place(); to my header so it fires on every load. If you need it to work within a dynamically loaded partial, try adding that line of js to your partial?

jp-ryuji commented 8 years ago

Encountered the same problem.

The following is a workaround I did (Fill placeholders when the data is drawed or changed.).

You should specify the place_holder option as follows even when you want to use the default one, '-'.

best_in_place(..., place_holder: '<some_place_holder>')
function fillPlaceHolderOfBestInPlace(that) {
  var $that = $(that),
      str   = $.trim($that.text());

  if (!str) {
    $that.text($that.data('bip-placeholder'));
  }
}

function fillEachPlaceHolderOfBestInPlace() {
  $('.best_in_place').each(function() {
    fillPlaceHolderOfBestInPlace(this);
  });
}

$(function() {
  table = $('<id_for_datatables>').DataTable({
    :
    :
    serverSide: true,
    ajax: '<ajax_endpoint>',
    deferRender: true,
    drawCallback: function (settings) {
      fillEachPlaceHolderOfBestInPlace();
    }
  });

  $('.best_in_place').best_in_place();

  $(document).on('ajax:success', '.best_in_place', function() {
    fillPlaceHolderOfBestInPlace(this);
  });
});
jalevin commented 7 years ago

For me, I had to re-initialize after the table loaded Ajax data.

$ ->
    $('#assets-table').dataTable
        pageLength: 100
        ajax: '/assets.json'
        serverSide: true
        order: true
        deferRender: true
        columns: [
            { "data": "filename" }
            { "data": "corrupt" }
        ]
        fnDrawCallback: (oSettings) ->
            $('.best_in_place').best_in_place()
andreas-it-dev commented 7 years ago

@jalevin you saved my day, thanks!