зеркало из https://github.com/mozilla/popcorn-js.git
[#270] Add XHR API dataType requirement for specific object returns
This commit is contained in:
Родитель
d6b8ab1c09
Коммит
742ed140bb
37
popcorn.js
37
popcorn.js
|
@ -813,7 +813,7 @@
|
|||
|
||||
Popcorn.xhr = function ( options ) {
|
||||
|
||||
if ( ( options.dataType || "" ).toLowerCase() === "jsonp" ) {
|
||||
if ( options.dataType && options.dataType.toLowerCase() === "jsonp" ) {
|
||||
|
||||
Popcorn.xhr.getJSONP(
|
||||
options.url,
|
||||
|
@ -823,9 +823,14 @@
|
|||
}
|
||||
|
||||
var settings = Popcorn.extend( {}, setup, options );
|
||||
|
||||
|
||||
// Create new XMLHttpRequest object
|
||||
settings.ajax = settings.xhr();
|
||||
|
||||
// Normalize dataType
|
||||
settings.dataType = settings.dataType.toLowerCase();
|
||||
|
||||
|
||||
if ( settings.ajax ) {
|
||||
|
||||
if ( settings.type === "GET" && settings.data ) {
|
||||
|
@ -865,6 +870,12 @@
|
|||
text: settings.ajax.responseText,
|
||||
json: json
|
||||
};
|
||||
|
||||
// If a dataType was specified, return that type of data
|
||||
if ( settings.dataType ) {
|
||||
data = data[ settings.dataType ];
|
||||
}
|
||||
|
||||
|
||||
settings.success.call( settings.ajax, data );
|
||||
|
||||
|
@ -881,13 +892,23 @@
|
|||
script = document.createElement("script"),
|
||||
paramStr = url.split("?")[1],
|
||||
fired = false,
|
||||
params, callback;
|
||||
params = [],
|
||||
callback;
|
||||
|
||||
script.src = url;
|
||||
|
||||
params = paramStr.split("&");
|
||||
if ( paramStr ) {
|
||||
params = paramStr.split("&");
|
||||
}
|
||||
|
||||
|
||||
callback = params.length ? params[ params.length - 1 ].split("=")[1] : "jsonp";
|
||||
|
||||
|
||||
if ( !paramStr ) {
|
||||
url += "?callback=" + callback;
|
||||
}
|
||||
|
||||
script.src = url;
|
||||
|
||||
callback = params.length ? params[ params.length - 1 ].split("=")[1] : Popcorn.guid("jsonp");
|
||||
|
||||
if ( callback ) {
|
||||
// define the jsonp success callback globally
|
||||
|
@ -909,7 +930,7 @@
|
|||
|
||||
head.insertBefore( script, head.firstChild );
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Exposes Popcorn to global context
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
<title>Popcorn API</title>
|
||||
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen">
|
||||
<script src="jquery.js"></script>
|
||||
<script src="jquery.message.js"></script>
|
||||
|
||||
<script src="qunit/qunit.js"></script>
|
||||
<!--
|
||||
do not move - this must be called immediately prior to
|
||||
|
|
|
@ -1021,7 +1021,7 @@ test("Text Response", function () {
|
|||
|
||||
expect(expects);
|
||||
|
||||
stop( 10000 );
|
||||
stop()
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.txt',
|
||||
|
@ -1037,6 +1037,37 @@ test("Text Response", function () {
|
|||
});
|
||||
});
|
||||
|
||||
test("dataType: Text Response", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop()
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.txt',
|
||||
dataType: "text",
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
equals( data, "This is a text test", "dataType: 'text', test.txt returns the string 'This is a text test'");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
test("JSON Response", function () {
|
||||
|
||||
var expects = 2,
|
||||
|
@ -1050,7 +1081,7 @@ test("JSON Response", function () {
|
|||
|
||||
expect(expects);
|
||||
|
||||
stop( 10000 );
|
||||
stop()
|
||||
|
||||
|
||||
var testObj = { "data": {"lang": "en", "length": 25} };
|
||||
|
@ -1071,9 +1102,10 @@ test("JSON Response", function () {
|
|||
|
||||
});
|
||||
|
||||
test("JSONP Response", function () {
|
||||
|
||||
var expects = 4,
|
||||
test("dataType: JSON Response", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
|
@ -1084,7 +1116,42 @@ test("JSONP Response", function () {
|
|||
|
||||
expect(expects);
|
||||
|
||||
stop( 10000 );
|
||||
stop()
|
||||
|
||||
|
||||
var testObj = { "data": {"lang": "en", "length": 25} };
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.js',
|
||||
dataType: "json",
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
|
||||
ok( QUnit.equiv(data, testObj) , "dataType: 'json', data returns an object of data");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test("JSONP Response", function () {
|
||||
|
||||
var expects = 6,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop();
|
||||
|
||||
|
||||
var testObj = { "data": {"lang": "en", "length": 25} };
|
||||
|
@ -1099,7 +1166,7 @@ test("JSONP Response", function () {
|
|||
plus();
|
||||
|
||||
|
||||
console.log(data);
|
||||
|
||||
ok( QUnit.equiv(data, testObj) , "Popcorn.xhr({}) data.json returns an object of data");
|
||||
plus();
|
||||
|
||||
|
@ -1123,6 +1190,22 @@ test("JSONP Response", function () {
|
|||
);
|
||||
|
||||
|
||||
Popcorn.xhr.getJSONP(
|
||||
'data/jsonp.json',
|
||||
|
||||
function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
|
||||
|
||||
ok( QUnit.equiv(data, testObj) , "Popcorn.xhr.getJSONP data.json returns an object of data");
|
||||
plus();
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
test("XML Response", function () {
|
||||
|
@ -1138,7 +1221,7 @@ test("XML Response", function () {
|
|||
|
||||
expect(expects);
|
||||
|
||||
stop( 10000 );
|
||||
stop()
|
||||
|
||||
|
||||
Popcorn.xhr({
|
||||
|
@ -1160,6 +1243,43 @@ test("XML Response", function () {
|
|||
|
||||
});
|
||||
|
||||
test("dataType: XML Response", function () {
|
||||
|
||||
var expects = 2,
|
||||
count = 0;
|
||||
|
||||
function plus() {
|
||||
if ( ++count === expects ) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
expect(expects);
|
||||
|
||||
stop()
|
||||
|
||||
|
||||
Popcorn.xhr({
|
||||
url: 'data/test.xml',
|
||||
dataType: "xml",
|
||||
success: function( data ) {
|
||||
|
||||
ok(data, "xhr returns data");
|
||||
plus();
|
||||
|
||||
var parser = new DOMParser(),
|
||||
xml = parser.parseFromString('<?xml version="1.0" encoding="UTF-8"?><dashboard><locations class="foo"><location for="bar"><infowindowtab> <tab title="Location"><![CDATA[blabla]]></tab> <tab title="Users"><![CDATA[blublu]]></tab> </infowindowtab> </location> </locations> </dashboard>',"text/xml");
|
||||
|
||||
|
||||
equals( data.toString(), xml.toString(), "dataType: 'xml', data.xml returns a document of xml");
|
||||
plus();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
module("Popcorn Parser");
|
||||
|
||||
test("Parsing Functions", function () {
|
||||
|
|
Загрузка…
Ссылка в новой задаче