Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Add new api.destroyed flag. Make sure AJAX plugin allows global ajax.…
Browse files Browse the repository at this point in the history
…success/error handlers to be defined, and any url selectors are always parsed before success() is called.
  • Loading branch information
Craga89 committed Apr 24, 2012
1 parent b4e7652 commit c490380
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 30 deletions.
6 changes: 5 additions & 1 deletion dist/jquery.qtip.basic.js
Expand Up @@ -9,7 +9,7 @@
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* Date: Mon Apr 9 17:49:18 2012 +0100
* Date: Tue Apr 10 19:58:28 2012 +0100
*/

/*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
Expand Down Expand Up @@ -174,6 +174,7 @@ function QTip(target, options, id, attr)
// Setup class attributes
self.id = id;
self.rendered = FALSE;
self.destroyed = FALSE;
self.elements = elements = { target: target };
self.timers = { img: {} };
self.options = options;
Expand Down Expand Up @@ -1504,6 +1505,9 @@ function QTip(target, options, id, attr)
title = $.attr(t, oldtitle),
elemAPI = target.data('qtip');

// Set flag the signify destroy is taking place to plugins
self.destroyed = TRUE;

// Destroy tooltip and any associated plugins if rendered
if(self.rendered) {
tooltip.stop(1,0).remove();
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.qtip.css
Expand Up @@ -9,7 +9,7 @@
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* Date: Mon Apr 9 17:49:18 2012 +0100
* Date: Tue Apr 10 19:58:28 2012 +0100
*/

/* Core qTip styles */
Expand Down
49 changes: 36 additions & 13 deletions dist/jquery.qtip.js
Expand Up @@ -9,7 +9,7 @@
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* Date: Mon Apr 9 17:49:18 2012 +0100
* Date: Tue Apr 10 19:58:28 2012 +0100
*/

/*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
Expand Down Expand Up @@ -174,6 +174,7 @@ function QTip(target, options, id, attr)
// Setup class attributes
self.id = id;
self.rendered = FALSE;
self.destroyed = FALSE;
self.elements = elements = { target: target };
self.timers = { img: {} };
self.options = options;
Expand Down Expand Up @@ -1504,6 +1505,9 @@ function QTip(target, options, id, attr)
title = $.attr(t, oldtitle),
elemAPI = target.data('qtip');

// Set flag the signify destroy is taking place to plugins
self.destroyed = TRUE;

// Destroy tooltip and any associated plugins if rendered
if(self.rendered) {
tooltip.stop(1,0).remove();
Expand Down Expand Up @@ -1994,10 +1998,10 @@ function Ajax(api)
var self = this,
tooltip = api.elements.tooltip,
opts = api.options.content.ajax,
defaults = QTIP.defaults.content.ajax,
namespace = '.qtip-ajax',
rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
first = TRUE,
destroyed = FALSE,
stop = FALSE,
xhr;

Expand Down Expand Up @@ -2053,21 +2057,29 @@ function Ajax(api)

// Define common after callback for both success/error handlers
function after() {
if(destroyed) { return; }
var complete;

// Don't proceed if tooltip is destroyed
if(api.destroyed) { return; }

// Set first flag to false
first = FALSE;

// Re-display tip if loading and first time, and reset first flag
if(hideFirst) { stop = TRUE; api.show(event.originalEvent); }

// Call users complete if it was defined
if($.isFunction(opts.complete)) { opts.complete.apply(this, arguments); }
// Call users complete method if it was defined
if((complete = defaults.success || opts.success) && $.isFunction(complete)) {
complete.apply(opts.context || api, arguments);
}
}

// Define success handler
function successHandler(content) {
if(destroyed) { return; }
function successHandler(content, status, jqXHR) {
var success;

// Don't proceed if tooltip is destroyed
if(api.destroyed) { return; }

if(selector) {
// Create a dummy div to hold the results and grab the selector element
Expand All @@ -2080,26 +2092,37 @@ function Ajax(api)
.find(selector);
}

// Set the content
api.set('content.text', content);
// Call the success function if one is defined
if((success = defaults.success || opts.success) && $.isFunction(success)) {
success.call(opts.context || api, content, status, jqXHR);
}

// Otherwise set the content
else { api.set('content.text', content); }
}

// Error handler
function errorHandler(xhr, status, error) {
if (destroyed || xhr.status === 0) { return; }
if(api.destroyed || xhr.status === 0) { return; }
api.set('content.text', status + ': ' + error);
}

// Setup $.ajax option object and process the request
xhr = $.ajax( $.extend({ success: successHandler, error: errorHandler, context: api }, opts, { url: url, complete: after }) );
xhr = $.ajax(
$.extend({
error: defaults.error || errorHandler,
context: api
},
opts, { url: url, success: successHandler, complete: after })
);
},

destroy: function() {
// Cancel ajax request if possible
if(xhr && xhr.abort) { xhr.abort(); }

// Set destroyed flag
destroyed = TRUE;
// Set api.destroyed flag
api.destroyed = TRUE;
}
});

Expand Down
6 changes: 3 additions & 3 deletions dist/jquery.qtip.min.js

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions src/ajax/ajax.js
Expand Up @@ -3,10 +3,10 @@ function Ajax(api)
var self = this,
tooltip = api.elements.tooltip,
opts = api.options.content.ajax,
defaults = QTIP.defaults.content.ajax,
namespace = '.qtip-ajax',
rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
first = TRUE,
destroyed = FALSE,
stop = FALSE,
xhr;

Expand Down Expand Up @@ -62,21 +62,29 @@ function Ajax(api)

// Define common after callback for both success/error handlers
function after() {
if(destroyed) { return; }
var complete;

// Don't proceed if tooltip is destroyed
if(api.destroyed) { return; }

// Set first flag to false
first = FALSE;

// Re-display tip if loading and first time, and reset first flag
if(hideFirst) { stop = TRUE; api.show(event.originalEvent); }

// Call users complete if it was defined
if($.isFunction(opts.complete)) { opts.complete.apply(this, arguments); }
// Call users complete method if it was defined
if((complete = defaults.success || opts.success) && $.isFunction(complete)) {
complete.apply(opts.context || api, arguments);
}
}

// Define success handler
function successHandler(content) {
if(destroyed) { return; }
function successHandler(content, status, jqXHR) {
var success;

// Don't proceed if tooltip is destroyed
if(api.destroyed) { return; }

if(selector) {
// Create a dummy div to hold the results and grab the selector element
Expand All @@ -89,26 +97,37 @@ function Ajax(api)
.find(selector);
}

// Set the content
api.set('content.text', content);
// Call the success function if one is defined
if((success = defaults.success || opts.success) && $.isFunction(success)) {
success.call(opts.context || api, content, status, jqXHR);
}

// Otherwise set the content
else { api.set('content.text', content); }
}

// Error handler
function errorHandler(xhr, status, error) {
if (destroyed || xhr.status === 0) { return; }
if(api.destroyed || xhr.status === 0) { return; }
api.set('content.text', status + ': ' + error);
}

// Setup $.ajax option object and process the request
xhr = $.ajax( $.extend({ success: successHandler, error: errorHandler, context: api }, opts, { url: url, complete: after }) );
xhr = $.ajax(
$.extend({
error: defaults.error || errorHandler,
context: api
},
opts, { url: url, success: successHandler, complete: after })
);
},

destroy: function() {
// Cancel ajax request if possible
if(xhr && xhr.abort) { xhr.abort(); }

// Set destroyed flag
destroyed = TRUE;
// Set api.destroyed flag
api.destroyed = TRUE;
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/core.js
Expand Up @@ -102,6 +102,7 @@ function QTip(target, options, id, attr)
// Setup class attributes
self.id = id;
self.rendered = FALSE;
self.destroyed = FALSE;
self.elements = elements = { target: target };
self.timers = { img: {} };
self.options = options;
Expand Down Expand Up @@ -1432,6 +1433,9 @@ function QTip(target, options, id, attr)
title = $.attr(t, oldtitle),
elemAPI = target.data('qtip');

// Set flag the signify destroy is taking place to plugins
self.destroyed = TRUE;

// Destroy tooltip and any associated plugins if rendered
if(self.rendered) {
tooltip.stop(1,0).remove();
Expand Down

0 comments on commit c490380

Please sign in to comment.