Search code examples
javascriptjquerycallbackjeditablejquery-callback

Supplying a callback to Jeditable


Summary: When I try supplying a onsubmit or onreset callback to Jeditable, I get Object [function] has no method 'apply' errors.

How I got here: I've been working on a rails plugin to supply Jeditable and jWYSIWYG for in-place WYSIWYG editing. Development is driven by a Rails project I'm working on which asks for specific functions.

One of the options I added was the ability to trigger Jeditable's edit mode using a button instead of clicking on the editable text itself, following the pattern suggested in this answer. The next step, though, is to hide the button while in edit mode, and reveal it again when leaving edit mode.

The hide is easy enough; I just added a line to the triggering function which sends .toggle() to the button. Reveal is trickier. I figure I need to .toggle() again on submit or cancel, and helpfully, Jeditable offers onsubmit and onreset callbacks.

However, when I try using those callbacks, I get this Object [something] has no method 'apply' errors.

What I'm trying: Because this is in the context of a Rails helper, the actual mechanics are a little more involved than this, but the upshot is that I'm trying to follow this pattern, handing Jeditable this in the args:

"onsubmit":"showTrigger",

and then including this script:

function showTrigger(settings, original) {
    $(".edit_trigger[id='element_id']").toggle();
}

However, on submitting changes or canceling an edit, I get the error

Object showTrigger has no method 'apply'

...as described above.

I also tried sending in a function directly as the "onsubmit" argument (i.e. "onsubmit": "function(settings, original){$(\".edit_trigger[id='element_id']\").toggle();}" and then I just get Object function(settings, original){$(\".edit_trigger[id='element_id']\").toggle();} has no method 'apply' instead.

There must be something wrong with how I'm handing in this callback. Any ideas?

ETA: This answer suggests to me that somehow I'm providing a string to Jeditable when it expects a function instead. However, because I'm working within the context of a Rails helper, I'm not at all sure how to fix that - the "showTrigger" bit is set as a Ruby variable in the helper, and although window.showTrigger() is defined when the window is loaded, I don't know how to designate that function within a Ruby variable such that it will be recognized as a function at page load time.


Solution

  • As my ETA above suggested, the problem did turn out to be passing a string rather than a function. My Rails helper was generating arguments for Jeditable by taking an argument hash and using .to_json. This made the string function name into, well, a quoted string. In order to provide this argument as a function name Jeditable would understand, I needed to include these specific arguments as a pre-JSON-ified argument.

    In other words, instead of defining a Ruby variable which contained a string with the name of the function, I defined it containing the JSON object with the name of the function. Instead of trigger_reset = 'showTrigger' I used trigger_reset = '{ onsave: showTrigger, onreset: showTrigger }'.

    Then, instead of including this as another argument in the args hash, where that hash was added to the args object in JS using $().extend(), I added the value of the trigger_reset variable as a third argument to extend().

    Of course, this turns out to all be bound up in code I didn't include in my original question, so it's no wonder nobody could help. Sorry, and thanks anyway.