#18 new
crankshaft

Ajax.InPlaceEditor doesn't support ajaxOptions onComplete

Reported by crankshaft | June 5th, 2008 @ 10:24 AM

I'm using CakePHP and its Ajax Helper in our project. To invoke the editor, you use the syntax below:

<?php echo $ajax->editor('myDivId', '/my_controller/updateField/field_name', array('onComplete'=>'function(){doSomething();}', 'highlightcolor'=>'#cccccc', 'highlightendcolor'=>'#959595')); ?>

This should generate the following JS:

new Ajax.InPlaceEditor('myDivId', '/my_controller/updateField/field_name', {onComplete:function(){doSomething();}, highlightcolor:'#cccccc', highlightendcolor:'#959595', ajaxOptions:{asynchronous:true, evalScripts:true}});

But due to a bug in CakePHP it completely strips out the onComplete stuff.

However, Cake supports another 'complete' option documented here. You use it like this:

<?php echo $ajax->editor('myDivId', '/my_controller/updateField/field_name', array('complete'=>'doSomething();', 'highlightcolor'=>'#cccccc', 'highlightendcolor'=>'#959595')); ?>

The code above generates the following JS:

new Ajax.InPlaceEditor('myDivId', '/my_controller/updateField/field_name', {highlightcolor:'#cccccc', highlightendcolor:'#959595', ajaxOptions:{asynchronous:true, evalScripts:true, onComplete:function(request, json) {doSomething();}}});

That looks promising, however, the onComplete in the ajaxOptions NEVER FIRES. I got it to work by commenting out line 634 in controls.js (scriptaculous 1.8.1):

      Object.extend(options, {
        parameters: params,
        //onComplete: this._boundWrapperHandler,
        onFailure: this._boundFailureHandler
      });

...but I don't think that's a good solution. :)

Comments and changes to this ticket

  • Thomas Fuchs

    Thomas Fuchs June 12th, 2008 @ 08:47 AM

    • → Assigned user changed from “Thomas Fuchs” to “Christophe Porteneuve”
  • crankshaft

    crankshaft June 18th, 2008 @ 03:25 PM

    I couldn't afford waiting any longer, so I had to fix this myself. :)

    The problem lies between lines 631-636 in control.js:

          var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions);
          Object.extend(options, {
            parameters: params,
            onComplete: this._boundWrapperHandler,
            onFailure: this._boundFailureHandler
          });
    

    Now, if this.options.ajaxOptions['onComplete'] != null, it gets written over when extending the options object. The solution is to "merge" the user's custom onComplete functions with this._boundWrapperHandler. I don't know if there's a clean way to do this, but I solved it like this:

          var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions);
          
          // patch begins
          var oc = new Function();
          for (var i in options) {
          	if (i == "onComplete") {
          		oc = options[i];
          	}
          }
          Object.extend(options, {
            parameters: params,
            onComplete: function() { this._boundWrapperHandler(); oc(); }.bind(this),
            onFailure: this._boundFailureHandler
          });
          // patch ends
    

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

script.aculo.us is an open-source JavaScript framework for visual effects and interface behaviours.

Source available from github
The Git repository resides at:
http://github.com/madrobby/scriptaculous

Check out the current development trunk with:
git clone git://github.com/madrobby/scriptaculous.git

As script.aculo.us 1.xx is feature-frozen, this development trunk is for bugfixes only.

New development should happen only for
script.aculo.us 2.

Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post a an example that shows off the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.