views:

142

answers:

1

I've created a plug-in that duck-punches the jQuery Ajax function. One problem that I cannot locate when running the Ajax unit tests for 1.4.2 It will not pass the two tests

  1. Ajax events with context
  2. jQuery.ajax context modification

Test one has a test that always fails:

 function callback(msg){
  return function(){
   equals( this, context, "context is preserved on callback " + msg );
  };
 }

The this reference instead of being the <div/> tag becomes an XPC Cross Origin Wrapper.

My Source Code is here:

My unit tests for the plug-in can be run here:

The failing unit tests can be run here:

One note is that jQuery 1.4.2 Ajax doe not handle a server response of 301 with FireFox 3.6.8. It will be returned as 0. I've modified my jQuery Library to account for this.

Also some JSONP stop watch timing. BUG #5383.

And maybe some presentation issues and this plug-in will be ready for the wild.

Any help would be appreciated. including design. Thanks in advance.

+5  A: 

The problem is you're losing context in your service.monitor methods when calling the originally passed success, error, beforeSend and complete handlers.

Take a look in ajaxMonitor.service.js, you need to replace each of the original handler invocations with a .call() to maintain the proper context:

1) In service.monitorSuccess():

origSuccess(data, textStatus, request);
//should be:
origSuccess.call(this, data, textStatus, request);

2) In service.monitorError():

origError(request, status, errorThrown);
//should be:
origError.call(this, request, status, errorThrown);

3) In service.monitorBeforeSend():

var abortEarly = origBeforeSend(request);
//should be:
var abortEarly = origBeforeSend.call(this, request);

4) In service.monitorComplete():

var xhr = origComplete(request, status);
//should be:
var xhr = origComplete.call(this, request, status);

I copied your test setup here so you can see the results: http://ncraver.com/AjaxTest/

Notice some other tests are failing...these are unrelated to my changes, they're the result of the my site not running PHP, so tests are getting an improper response from those .php pages :)

Nick Craver
@Nick - way to go. Damn it I was almost there. you can see that in my code when I call original Ajax. Very good call. I am indebted to you worth every bounty point. Now I need to pass this on through to my mocker.
Gutzofter
@Nick - as a side note I was wondering if you could pass onto me any criticism about the code and the tool itself. feedback is very important. Maybe I should use SO to get the feedback. It can be subjective maybe something like this can go into area 51?
Gutzofter
@Gutzofter - I'm not sure what the proper site is, for me the tools is looking real good as is, but as a disclaimer: I'm not a *huge* TDD guy (usually a lack of dev cycle time to adjust/do it right, unfortunately). I would take off the `jquery-plugins` tag on this question and replace it with `javascript`, that should get more eyes on it, as the TDD thing is overall more of a JavaScript deal than jQuery specific, some of those guys might be able to share some additional thoughts. Also give send a link to `@jquery` on twitter when you're ready, the team will probably take a look :)
Nick Craver
The initial idea for this tool was implemented in one-weekend. If it wasn't for running the jQuery Ajax unit tests with the plug-in installed. I wouldn't of have found this **Major Bug**. Since I'm going to be adding a change/bug log I would like to give mention to you fixing this. Do you want any links or emails with your name?
Gutzofter
@Gutzofter - Thanks, no need though, if you can link the final release as a comment to this and the question though I'd like to take another look at the final, as will others that find this in the future :)
Nick Craver