Supplying a callback to Jeditable

Posted by pjmorse on Stack Overflow See other posts from Stack Overflow or by pjmorse
Published on 2012-11-16T19:26:34Z Indexed on 2012/11/19 17:00 UTC
Read the original article Hit count: 324

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery