automatically determine if we should use JSONP or something else. popcorn-port
authorhamidouk
Tue, 13 Dec 2011 15:14:15 +0100
branchpopcorn-port
changeset 452 35495f156c41
parent 451 2217bd6fd068
child 453 8568e47379a2
automatically determine if we should use JSONP or something else.
src/js/data.js
unittests/tests/dataloader.js
--- a/src/js/data.js	Tue Dec 13 10:27:23 2011 +0100
+++ b/src/js/data.js	Tue Dec 13 15:14:15 2011 +0100
@@ -35,7 +35,17 @@
                   }
       };
       
-      IriSP.jQuery.get(url, IriSP.wrap(this, func));                                
+      /* automagically choose between json and jsonp */
+      if (url.indexOf(document.location.hostname) === -1 &&
+          url[0] !== '/' /* not a relative url */ ) {
+        // we contacting a foreign domain, use JSONP
+
+        IriSP.jQuery.get(url, {}, IriSP.wrap(this, func), "jsonp");
+      } else {
+
+        // otherwise, hey, whatever rows your boat
+        IriSP.jQuery.get(url, IriSP.wrap(this, func));
+      }
     
     } else {
       /* simply push the callback - it'll get called when the ressource
--- a/unittests/tests/dataloader.js	Tue Dec 13 10:27:23 2011 +0100
+++ b/unittests/tests/dataloader.js	Tue Dec 13 15:14:15 2011 +0100
@@ -41,5 +41,15 @@
     ok(spy_callback.calledTwice && xhr.requests.length === 1, "callback called twice but request made only once.");
     
   });
+ 
+  test("should default to JSONP for foreign domains", function() {
+    /* we can't simulate jsonp so we just verify that the function is called */
+    var stub = this.stub(IriSP.jQuery, "ajax");
+    var dt = new IriSP.DataLoader();
     
-}
\ No newline at end of file
+    var resp = dt.get("http://example.com/url&a=1", stub);
+
+    ok(stub.calledOnce, "ajax request actually made");
+  });
+   
+}