diff --git a/lib/torque/provider.json.js b/lib/torque/provider.json.js index 9319533..5c27e39 100644 --- a/lib/torque/provider.json.js +++ b/lib/torque/provider.json.js @@ -343,13 +343,26 @@ }; var url = this._tilerHost() + "/tiles/layergroup"; var extra = this._extraParams(); - torque.net.post( url + (extra ? "?" + extra: ''), JSON.stringify(layergroup) , function (req) { + + // tiler needs map_key instead of api_key + // so replace it + if (extra) { + extra = extra.replace('api_key=', 'map_key='); + } + + url = url + + "?config=" + encodeURIComponent(JSON.stringify(layergroup)) + + "&callback=?" + (extra ? "&" + extra: ''); + + torque.net.jsonp(url, function (data) { var query = format("select * from ({sql}) __torque_wrap_sql limit 0", { sql: self.getSQL() }); self.sql(query, function (queryData) { - callback({ - updated_at: JSON.parse(req.response).last_updated, - fields: queryData.fields - }); + if (data) { + callback({ + updated_at: data.last_updated, + fields: queryData.fields + }); + } }, { parseJSON: true }); }); }, diff --git a/lib/torque/request.js b/lib/torque/request.js index 4d5abbe..6e14690 100644 --- a/lib/torque/request.js +++ b/lib/torque/request.js @@ -4,6 +4,41 @@ var lastCall = null; + function jsonp(url, callback, options) { + options = options || { timeout: 10000 }; + var head = document.getElementsByTagName('head')[0]; + var script = document.createElement('script'); + + // function name + var fnName = 'torque_' + Date.now(); + + function clean() { + head.removeChild(script); + clearTimeout(timeoutTimer); + delete window[fnName]; + } + + window[fnName] = function() { + clean(); + callback.apply(window, arguments); + }; + + // timeout for errors + var timeoutTimer = setTimeout(function() { + clean(); + callback.call(window, null); + }, options.timeout); + + // setup url + url = url.replace('callback=\?', 'callback=' + fnName); + script.type = 'text/javascript'; + script.src = url; + script.async = true; + // defer the loading because IE9 loads in the same frame the script + // so Loader._script is null + setTimeout(function() { head.appendChild(script); }, 0); + } + function get(url, callback, options) { options = options || { method: 'GET', @@ -52,6 +87,7 @@ torque.net = { get: get, post: post, + jsonp: jsonp, lastCall: function() { return lastCall; } }; diff --git a/test/request.js b/test/request.js new file mode 100644 index 0000000..d9d46ea --- /dev/null +++ b/test/request.js @@ -0,0 +1,30 @@ +module('request') + +asyncTest("json", 6, function() { + var called = null; + torque.net.jsonp('http://test.com?callback=?', function(test) { + called = arguments; + }); + + setTimeout(function() { + var scripts = document.getElementsByTagName('script'); + var found = null; + for (var i = 0 ; !found && i < scripts.length; ++i) { + var s = scripts[i]; + if (s.getAttribute('src').indexOf('test.com') !== -1) { + found = s; + } + } + var src = found.getAttribute('src'); + var fnName = src.match(/torque_.*/); + window[fnName]('test1', 2, null); + equal(src.indexOf('http://test.com?callback=torque_'), 0); + equal(called[0], 'test1'); + equal(called[1], 2); + equal(called[2], null); + equal(found.parent, null); + equal(window[fnName], undefined); + QUnit.start(); + }, 5); + +}); diff --git a/test/suite.html b/test/suite.html index b4b4352..5b62145 100644 --- a/test/suite.html +++ b/test/suite.html @@ -19,5 +19,6 @@ +