Skip to content

changing beforeSend to be more accurate according to the jquery spec#73

Closed
yairgo wants to merge 1 commit intorails:masterfrom
yairgo:master
Closed

changing beforeSend to be more accurate according to the jquery spec#73
yairgo wants to merge 1 commit intorails:masterfrom
yairgo:master

Conversation

@yairgo
Copy link

@yairgo yairgo commented Jan 7, 2011

We are working on a part of our website and we needed to tie into the jquery global beforeSend function. While trying to use this method we were trying to cancel the ajax request in certain scenarios.

If you put in
$('a').live('ajax:beforeSend', function(event, xhr) {
return false;
});

Then the ajax request is not sent. With this change I also noticed that on line 44 $this was being assigned to $(this), but the variable el is already assigned to the equivalent of $(this), so I removed that variable.

I've also changed the jquery global beforeSend call back to be called with the same parameters that are used if you call before send without the beforeSend in rails.js

return beforeSendGlobalCallback.call(this, xhr, settings, el);
The first argument(this) ensures that the global beforeSend is called within the same context as it would have been called in if the beforeSend is not in the rails.js file. The second argument was already being passed along. The third argument is specified in the jquery docs as settings, just passing that along as is.
And last but not least, I'm passing the element that made the ajax call as the 4th argument.

This commit is just making the beforeSend method conform to the way the global beforeSend works, except for passing the element as the forth argument. I didn't see any tests around what arguments are passed to the beforeSend method but I did add a test to confirm returning false from the global beforeSend works as expected.

I would also like to make it so that the complete(and possibly error) callback will call the global callback, but I didn't want to make too many changes per pull request.

What do you guys think about this pull request? Would you be willing to accept a pull request allowing complete and or error to call the global callback as well?

…t are specified by the jquery documentation.

on line 44 removed the variable $this because the variable el is the same thing. changed line 52 to reflect this change.
on line 58
  change rails#1 return the value that the global beforeSend returns.  according to the jquery spec if this returns false the ajax request is aborted but that was not currently happening
  change rails#2 call the global beforeSend call with the context of the current beforeSend method to keep it consistent.  pass in the settings param to keep consistent with jquery spec.  also pass in the element which may not be necessary for all scenarios but has proven very necessary in our environment.
@mislav
Copy link
Member

mislav commented Jan 7, 2011

Hello, thanks for the pull request. However, this has been fixed in a somewhat cleaner manner in 0ff66c4

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants