# HG changeset patch # User hamidouk # Date 1323785655 -3600 # Node ID 35495f156c4130a7382d4cf61712c76cca1a7c35 # Parent 2217bd6fd068fa454657e902262981e584a4f83f automatically determine if we should use JSONP or something else. diff -r 2217bd6fd068 -r 35495f156c41 src/js/data.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 diff -r 2217bd6fd068 -r 35495f156c41 unittests/tests/dataloader.js --- 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"); + }); + +}