Skip to content
Permalink
Browse files

Data Ajax: Added support for CORS and a fallback when CORS isn't supp…

…orted
  • Loading branch information...
LaurentGoderre committed Oct 28, 2014
1 parent 7701f81 commit 0fdd4bfb8eba71625b74ea1bf1e5a57448226856
Showing with 25 additions and 8 deletions.
  1. +1 −0 Gruntfile.coffee
  2. +24 −8 src/plugins/data-ajax/data-ajax.js
@@ -977,6 +977,7 @@ module.exports = (grunt) ->
"elem_details"
"elem_progress_meter"
"mathml"
"cors"
]
parseFiles: false
matchCommunityTests: false
@@ -43,6 +43,7 @@ var componentName = "wb-data-ajax",
var elm = wb.init( event, componentName + "-" + ajaxType, selector );

if ( elm ) {

ajax.apply( this, arguments );

// Identify that initialization has completed
@@ -52,13 +53,29 @@ var componentName = "wb-data-ajax",

ajax = function( event, ajaxType ) {
var elm = event.target,
$elm = $( elm );
$elm = $( elm ),
settings = window[ componentName ],
url = elm.getAttribute( "data-ajax-" + ajaxType ),
fetchObj = {
url: url
},
urlParts;

// Detect CORS requests
if ( settings && url.substr( 0, 4 ) === "http" ) {
urlParts = wb.getUrlParts( url );
if ( ( wb.pageUrlParts.protocol !== urlParts.protocol || wb.pageUrlParts.host !== urlParts.host ) && ( !Modernizr.cors || settings.forceCorsFallback ) ) {
if ( typeof settings.corsFallback === "function" ) {
fetchObj.dataType = "jsonp";
fetchObj.jsonp = "callback";
fetchObj = settings.corsFallback(fetchObj);
}
}
}

$elm.trigger({
type: "ajax-fetch.wb",
fetch: {
url: elm.getAttribute( "data-ajax-" + ajaxType )
}
fetch: fetchObj
});
};

@@ -72,7 +89,7 @@ $document.on( "timerpoke.wb " + initEvent + " " + updateEvent + " ajax-fetched.w
"prepend"
],
len = ajaxTypes.length,
$elm, ajaxType, i, content, pointer;
$elm, ajaxType, i, content;

for ( i = 0; i !== len; i += 1 ) {
ajaxType = ajaxTypes[ i ];
@@ -97,9 +114,8 @@ $document.on( "timerpoke.wb " + initEvent + " " + updateEvent + " ajax-fetched.w
$elm = $( eventTarget );

// ajax-fetched event
pointer = event.fetch.pointer;
if ( pointer ) {
content = pointer.html();
content = event.fetch.response;
if ( content ) {

// "replace" is the only event that doesn't map to a jQuery function
if ( ajaxType === "replace") {

0 comments on commit 0fdd4bf

Please sign in to comment.
You can’t perform that action at this time.