Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the jsonp callback context error. #67

Merged
merged 1 commit into from Apr 10, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 14 additions & 13 deletions jquery.mockjax.js
Expand Up @@ -31,7 +31,7 @@
}

try {
var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
if ( $.isXMLDoc( xmlDoc ) ) {
var err = $('parsererror', xmlDoc);
if ( err.length == 1 ) {
Expand Down Expand Up @@ -142,9 +142,9 @@
return function() {
return (function() {
// The request has returned
this.status = mockHandler.status;
this.status = mockHandler.status;
this.statusText = mockHandler.statusText;
this.readyState = 4;
this.readyState = 4;

// We have an executable function, call it to give
// the mock handler a chance to update it's data
Expand Down Expand Up @@ -270,7 +270,7 @@

requestSettings.dataType = "json";
if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
createJsonpCallback(requestSettings, mockHandler);
createJsonpCallback(requestSettings, mockHandler, origSettings);

// We need to make sure
// that a JSONP style response is executed properly
Expand Down Expand Up @@ -328,8 +328,8 @@
}

// Successful response
jsonpSuccess( requestSettings, mockHandler );
jsonpComplete( requestSettings, mockHandler );
jsonpSuccess( requestSettings, callbackContext, mockHandler );
jsonpComplete( requestSettings, callbackContext, mockHandler );

// If we are running under jQuery 1.5+, return a deferred object
if(jQuery.Deferred){
Expand All @@ -346,7 +346,8 @@


// Create the required JSONP callback function for the request
function createJsonpCallback( requestSettings, mockHandler ) {
function createJsonpCallback( requestSettings, mockHandler, origSettings ) {
var callbackContext = origSettings && origSettings.context || requestSettings;
jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);

// Replace the =? sequence both in the query string and the data
Expand All @@ -360,8 +361,8 @@
// Handle JSONP-style loading
window[ jsonp ] = window[ jsonp ] || function( tmp ) {
data = tmp;
jsonpSuccess( requestSettings, mockHandler );
jsonpComplete( requestSettings, mockHandler );
jsonpSuccess( requestSettings, callbackContext, mockHandler );
jsonpComplete( requestSettings, callbackContext, mockHandler );
// Garbage collect
window[ jsonp ] = undefined;

Expand All @@ -376,7 +377,7 @@
}

// The JSONP request was successful
function jsonpSuccess(requestSettings, mockHandler) {
function jsonpSuccess(requestSettings, callbackContext, mockHandler) {
// If a local callback was specified, fire it and pass it the data
if ( requestSettings.success ) {
requestSettings.success.call( callbackContext, ( mockHandler.response ? mockHandler.response.toString() : mockHandler.responseText || ''), status, {} );
Expand All @@ -389,7 +390,7 @@
}

// The JSONP request was completed
function jsonpComplete(requestSettings, mockHandler) {
function jsonpComplete(requestSettings, callbackContext, mockHandler) {
// Process result
if ( requestSettings.complete ) {
requestSettings.complete.call( callbackContext, {} , status );
Expand Down Expand Up @@ -481,7 +482,7 @@
//type: 'GET',
log: function(msg) {
window['console'] && window.console.log && window.console.log(msg);
},
},
status: 200,
statusText: "OK",
responseTime: 500,
Expand Down Expand Up @@ -518,4 +519,4 @@
return mockHandlers[i];
}
};
})(jQuery);
})(self.jQuery);