best_in_place Javascript Callbacks


I’m using the best_in_place gem for a project so that users can click on fields to edit them inline without visiting a separate “edit” page or doing a full page submit+reload. It’s a very nice library, but one problem I encountered was running some extra Javascript once the Ajax request succeeded or failed, e.g. to update a different part of the page based on what the user entered.

This isn’t documented, but it turns out that best_in_place fires the standard Rails ajax:success event, so you can listen for it. But it’s not called on the form that best_in_place uses to show an input element; it’s called on the original span created when you say best_in_place in your view. You can target this by wrapping it in another div or span, so your Javascript code would look like this:

$(function() {
  $('.whatever-you-like .best-in-place').live('ajax:success', function(event, data, status, xhr) {
    alert("Got a callback!");

From reading the code, it looks like best_in_place does not fire an event on failure. Maybe I’ll patch that and submit a pull request. . . .

blog comments powered by Disqus Prev: Chef Data Bag IDs Next: Emotional Ruby