Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way would be to wrap the code want to run in a function and then have that function call your onSuccess code. Obviously this wrapped function would have to be returned from a function:</p> <pre><code>var checkDriftOnSuccess = function(onSuccess) { return function(transport) { var dateHeader = transport.getHeader('Date'); if (!dateHeader) { USER.serverDrift = 0; } else { USER.serverDrift = (new Date(dateHeader).getTime() - (new Date().getTime())) / 1000; } onSuccess(transport); }; }; </code></pre> <p>And you'd call it like this:</p> <pre><code>new Ajax.Request( 'someLink.php', { onSuccess: checkDriftOnSuccess(function(transport) { // application specific code here }) } ); </code></pre> <p><strong>UPDATE</strong>: another way, by replacing the Prototype constructor. Not favored by me (a) because I don't know the Prototype codebase, and (b) there may be <code>instanceOf</code> issues, and (c) I haven't actually tested it.</p> <pre><code>var ajaxReplacer = (function() { var oldRequest; var checkDriftOnSuccess = function(onSuccess) { return function(transport) { var dateHeader = transport.getHeader('Date'); if (!dateHeader) { USER.serverDrift = 0; } else { USER.serverDrift = (new Date(dateHeader).getTime() - (new Date().getTime())) / 1000; } onSuccess(transport); }; }; // save the real constructor oldRequest = Ajax.Request; // define a new constructor that wraps the onSuccess callback Ajax.Request = function(url, options) { if (options.onSuccess) { options.onSuccess = checkDriftOnSuccess(options.onSuccess); } // call the real constructor return new oldRequest(url, options); // alternately, call the real constructor using a function call // return oldRequest.call(this, url, options); }; Ajax.Request.prototype = oldRequest.prototype; return { reset: function() { Ajax.Request = oldRequest; } }; }()); </code></pre> <p>In essence it creates a new object called ajaxReplacer. This has one method, <code>reset</code> that returns everything to the default Prototype behavior (a one-shot deal). Creating the object will replace the ajax request constructor to one that calls the special code in <code>onSuccess</code>.</p> <p>Note I have two ways of calling the old code. I do not know which one will work properly since I don't use Prototype. Test, test, and test again. If it works, great. If it doesn't, maybe I forgot to do something, or it gives you enough ideas to really do it, or you just can't do this easily.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload