Popcorn.xhr = function ( options ) {
- if ( ( options.dataType || "" ).toLowerCase() === "jsonp" ) {
+ if ( options.dataType && options.dataType.toLowerCase() === "jsonp" ) {
Popcorn.xhr.getJSONP(
options.url,
}
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 ) {
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 );
script = document.createElement("script"),
paramStr = url.split("?")[1],
fired = false,
- params, callback;
-
- script.src = url;
+ params = [],
+ callback;
- 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
head.insertBefore( script, head.firstChild );
- }
+ };
// Exposes Popcorn to global context
expect(expects);
- stop( 10000 );
+ stop()
Popcorn.xhr({
url: 'data/test.txt',
});
});
+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,
expect(expects);
- stop( 10000 );
+ stop()
var testObj = { "data": {"lang": "en", "length": 25} };
});
+
+test("dataType: JSON Response", function () {
+
+ var expects = 2,
+ count = 0;
+
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+ }
+ }
+
+ expect(expects);
+
+ 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 = 4,
+ var expects = 6,
count = 0;
function plus() {
expect(expects);
- stop( 10000 );
+ stop();
var testObj = { "data": {"lang": "en", "length": 25} };
plus();
- console.log(data);
+
ok( QUnit.equiv(data, testObj) , "Popcorn.xhr({}) data.json returns an object of data");
plus();
);
+ 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 () {
expect(expects);
- stop( 10000 );
+ stop()
Popcorn.xhr({
});
+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 () {