]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#270] Add XHR API dataType requirement for specific object returns
authorrwldrn <waldron.rick@gmail.com>
Wed, 19 Jan 2011 21:29:42 +0000 (16:29 -0500)
committerAnna Sobiepanek <annaopensource@gmail.com>
Thu, 20 Jan 2011 01:49:37 +0000 (20:49 -0500)
popcorn.js
test/index.html
test/popcorn.unit.js

index 3b5abbc12a723cc052c6c9c730f4606a4fcb9c26..c1957c35bbfddf9b5459f747e9c34c36045f1ed6 100644 (file)
   
   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
index db5f97980c07171e6e2b2fe1349cd531284fe81e..d5af7392fe1ebe8064f8f414f46b4805a3af1419 100644 (file)
@@ -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 
index ae85b812f879ba741d3c3de4148a6651d7852ecf..9f1062c01f29d3953c5b45ccaf150918ea68cc14 100644 (file)
@@ -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,45 @@ test("JSON Response", function () {
 
 });
 
+
+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() {
@@ -1084,7 +1151,7 @@ test("JSONP Response", function () {
   
   expect(expects);
   
-  stop( 10000 );
+  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 () {