]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#220] Popcorn.xhr support needed for parsing data-timeline-sources
authorrwldrn <waldron.rick@gmail.com>
Wed, 15 Dec 2010 19:30:35 +0000 (14:30 -0500)
committerrwldrn <waldron.rick@gmail.com>
Wed, 15 Dec 2010 19:30:35 +0000 (14:30 -0500)
popcorn.js
test/data/json.js [new file with mode: 0644]
test/data/test.js [new file with mode: 0644]
test/data/test.txt [new file with mode: 0644]
test/data/test.xml [new file with mode: 0644]
test/popcorn.unit.js

index 6268d7884b3b6e830d25b052cdb9452d576ca5f9..7be51922191445fe06256cc5c446b651111a80db 100644 (file)
     return plugin;
   };
   
+  
+  var setup = {
+    url: '',
+    data: '',
+    dataType: '',
+    success: Popcorn.nop,
+    type: 'GET',
+    async: true, 
+    xhr: function()  {
+      return new XMLHttpRequest();
+    }
+  };   
+  
+  Popcorn.xhr = function ( options ) {
+
+    var settings = Popcorn.extend( {}, setup, options );
+
+    settings.ajax  = settings.xhr();
+    
+    if ( settings.ajax ) {
+
+      settings.ajax.open( settings.type, settings.url, settings.async ); 
+      settings.ajax.send( null ); 
+
+      return Popcorn.xhr.httpData( settings );
+    }       
+  };
+
+  
+  Popcorn.xhr.httpData = function ( settings ) {
+  
+    var data, json = null,  
+        
+    onreadystatechange = settings.ajax.onreadystatechange = function() {
+
+      if ( settings.ajax.readyState == 4 ) { 
+        
+        try {
+          json = JSON.parse(settings.ajax.responseText);
+        } catch(e) {
+          //suppress
+        };
+
+        data = {
+          xml: settings.ajax.responseXML, 
+          text: settings.ajax.responseText, 
+          json: json
+        };
+
+        settings.success.call( settings.ajax, data );
+        
+      } 
+    }; 
+    return data;  
+  };
+  
 
   global.Popcorn = Popcorn;
   
diff --git a/test/data/json.js b/test/data/json.js
new file mode 100644 (file)
index 0000000..fa798a0
--- /dev/null
@@ -0,0 +1 @@
+{ "data": {"lang": "en", "length": 25} }
\ No newline at end of file
diff --git a/test/data/test.js b/test/data/test.js
new file mode 100644 (file)
index 0000000..fa798a0
--- /dev/null
@@ -0,0 +1 @@
+{ "data": {"lang": "en", "length": 25} }
\ No newline at end of file
diff --git a/test/data/test.txt b/test/data/test.txt
new file mode 100644 (file)
index 0000000..1f0886a
--- /dev/null
@@ -0,0 +1 @@
+This is a text test
\ No newline at end of file
diff --git a/test/data/test.xml b/test/data/test.xml
new file mode 100644 (file)
index 0000000..10f6b33
--- /dev/null
@@ -0,0 +1,11 @@
+<?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>
index 28352c977714a8e95cc5787a16e1939e8dddb985..46ab0adb8d563c623a14afb2b82b471d7c1600f0 100644 (file)
@@ -662,8 +662,120 @@ test("Protected Names", function () {
     };
   });
 });
+Popcorn.xhr.httpData
+
+module("Popcorn XHR");
+test("Basic", function () {
+  
+  expect(2);
+  
+  equals( typeof Popcorn.xhr, "function" , "Popcorn.xhr is a provided utility function");
+  equals( typeof Popcorn.xhr.httpData, "function" , "Popcorn.xhr.httpData is a provided utility function");
+  
+
+});
+
+test("Parsing text", function () {
+
+  var expects = 2, 
+      count = 0;
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+    }
+  }
+  
+  expect(expects);
+  
+  stop();
+
+  Popcorn.xhr({
+    url: 'data/test.txt', 
+    success: function( data ) {
+      
+      ok(data, "xhr returns data");
+      plus();
+      
+      equals( data.text, "This is a text test", "test.txt returns the string 'This is a text test'");
+      plus();
+      
+    }
+  });
+});
+
+test("Parsing JSON", 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', 
+    success: function( data ) {
+      
+      ok(data, "xhr returns data");
+      plus();
+      
+      
+      ok( QUnit.equiv(data.json, testObj) , "data.json returns an object of data");
+      plus();
+      
+    }
+  });
+
+});
+
+test("Parsing XML", function () {
+
+  var expects = 2, 
+      count = 0;
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+    }
+  }
+  
+  expect(expects);
+  
+  stop();
+
+
+  Popcorn.xhr({
+    url: 'data/test.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.xml.toString(), xml.toString(), "data.xml returns a document of xml");
+      plus();
+      
+    }
+  });
+
+});
+
 
 
+module("Popcorn Test Runner End");
 test("Last Check", function () {
   
   //   ALWAYS RUN LAST