mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX jQuery 1.5 compatibility for jQuery.ondemand, by using jQuery.Deferred and the 'beforeSend' callback instead of monkeypatching jQuery.ajax
This commit is contained in:
parent
31d7a00eb7
commit
c6af46f066
@ -1,11 +1,17 @@
|
||||
/**
|
||||
* On-demand JavaScript handler
|
||||
*
|
||||
* Based on http://plugins.jquery.com/files/issues/jquery.ondemand.js_.txt
|
||||
* and heavily modified to integrate with SilverStripe and prototype.js.
|
||||
* Adds capabilities for custom X-Include-CSS and X-Include-JS HTTP headers
|
||||
* to request loading of externals alongside an ajax response.
|
||||
*
|
||||
* IMPORTANT: This plugin monkeypatches the jQuery.ajax() method.
|
||||
* Requires jQuery 1.5 ($.Deferred support)
|
||||
*
|
||||
* CAUTION: Relies on customization of the 'beforeSend' callback in jQuery.ajaxSetup()
|
||||
*
|
||||
* @author Ingo Schommer (ingo at silverstripe dot com)
|
||||
* @author Sam Minnee (sam at silverstripe dot com)
|
||||
*/
|
||||
(function($){
|
||||
|
||||
@ -74,12 +80,12 @@
|
||||
/**
|
||||
* Process the X-Include-CSS and X-Include-JS headers provided by the Requirements class
|
||||
*/
|
||||
processOnDemandHeaders: function(xml, status, _ondemandComplete) {
|
||||
var self = this;
|
||||
processOnDemandHeaders: function(xml, status, xhr) {
|
||||
var self = this, processDfd = new $.Deferred();
|
||||
|
||||
// CSS
|
||||
if(xml.getResponseHeader('X-Include-CSS')) {
|
||||
var cssIncludes = xml.getResponseHeader('X-Include-CSS').split(',');
|
||||
if(xhr.getResponseHeader('X-Include-CSS')) {
|
||||
var cssIncludes = xhr.getResponseHeader('X-Include-CSS').split(',');
|
||||
for(var i=0;i<cssIncludes.length;i++) {
|
||||
// Syntax: "URL:##:media"
|
||||
if(cssIncludes[i].match(/^(.*):##:(.*)$/)) {
|
||||
@ -93,8 +99,8 @@
|
||||
|
||||
// JavaScript
|
||||
var newJsIncludes = [];
|
||||
if(xml.getResponseHeader('X-Include-JS')) {
|
||||
var jsIncludes = xml.getResponseHeader('X-Include-JS').split(',');
|
||||
if(xhr.getResponseHeader('X-Include-JS')) {
|
||||
var jsIncludes = xhr.getResponseHeader('X-Include-JS').split(',');
|
||||
for(var i=0;i<jsIncludes.length;i++) {
|
||||
if(!$.isItemLoaded(jsIncludes[i])) {
|
||||
newJsIncludes.push(jsIncludes[i]);
|
||||
@ -121,7 +127,7 @@
|
||||
async: false
|
||||
});
|
||||
} else {
|
||||
_ondemandComplete(xml, status);
|
||||
processDfd.resolve(xml, status, xhr);
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,44 +135,38 @@
|
||||
getScriptQueue();
|
||||
} else {
|
||||
// If there aren't any new includes, then we can just call the callbacks ourselves
|
||||
_ondemandComplete(xml, status);
|
||||
processDfd.resolve(xml, status, xhr);
|
||||
}
|
||||
|
||||
return processDfd.promise();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Ajax requests are amended to look for X-Include-JS and X-Include-CSS headers
|
||||
*/
|
||||
_originalAjax = $.ajax;
|
||||
$.ajax = function(s) {
|
||||
// Avoid recursion in ajax callbacks caused by getScript(), by not parsing
|
||||
// ondemand headers for 'script' datatypes
|
||||
if(s.dataType == 'script') return _originalAjax(s);
|
||||
$.ajaxSetup({
|
||||
// beforeSend is the only place to access the XHR object before success handlers are added
|
||||
beforeSend: function(jqXHR, s) {
|
||||
// Avoid recursion in ajax callbacks caused by getScript(), by not parsing
|
||||
// ondemand headers for 'script' datatypes
|
||||
if(s.dataType == 'script') return;
|
||||
|
||||
var _complete = s.complete;
|
||||
var _success = s.success;
|
||||
var _dataType = s.dataType;
|
||||
var dfd = new $.Deferred();
|
||||
|
||||
// This replaces the usual ajax success & complete handlers. They are called after any on demand JS is loaded.
|
||||
var _ondemandComplete = function(data, status) {
|
||||
//var status = $.httpSuccess(xml) ? 'success' : 'error'; //rewrote success function as httpSuccess doesn't exist anymore in jQuery 1.5
|
||||
if (typeof status == 'undefined' || status != 'success') status = 'error';
|
||||
// Register our own success handler (assumes no handlers are already registered)
|
||||
// 'success' is an alias for 'done', which is executed by the built-in deferred instance in $.ajax()
|
||||
jqXHR.success(function(success, statusText, jXHR) {
|
||||
$.processOnDemandHeaders(success, statusText, jXHR).done(function() {
|
||||
dfd.resolveWith(s.context || this, [success, statusText, jXHR]);
|
||||
});
|
||||
});
|
||||
|
||||
if(status == 'success') {
|
||||
//var data = jQuery.httpData(xml, _dataType); //unnecessary as data type conversion is automatically done in jQuery 1.5 using the ajaxConvert method
|
||||
if(_success) _success(data, status);
|
||||
// Reroute all external success hanlders through our own deferred.
|
||||
// Not overloading fail() as no event can cause the original request to fail.
|
||||
jqXHR.success = function(callback) {
|
||||
dfd.done(callback);
|
||||
}
|
||||
if(_complete) _complete(data, status);
|
||||
}
|
||||
});
|
||||
|
||||
// We remove the success handler and take care of calling it outselves within _ondemandComplete
|
||||
s.success = null;
|
||||
s.complete = function(xml, status) {
|
||||
$.processOnDemandHeaders(xml, status, _ondemandComplete);
|
||||
}
|
||||
|
||||
return _originalAjax(s);
|
||||
}
|
||||
|
||||
})(jQuery);
|
Loading…
Reference in New Issue
Block a user