fixed provider.json using jsonp

This commit is contained in:
javi 2013-11-09 10:12:37 +01:00
parent 135aecbfc4
commit 2d7bbc7afe
4 changed files with 85 additions and 5 deletions

View File

@ -343,13 +343,26 @@
}; };
var url = this._tilerHost() + "/tiles/layergroup"; var url = this._tilerHost() + "/tiles/layergroup";
var extra = this._extraParams(); 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() }); var query = format("select * from ({sql}) __torque_wrap_sql limit 0", { sql: self.getSQL() });
self.sql(query, function (queryData) { self.sql(query, function (queryData) {
callback({ if (data) {
updated_at: JSON.parse(req.response).last_updated, callback({
fields: queryData.fields updated_at: data.last_updated,
}); fields: queryData.fields
});
}
}, { parseJSON: true }); }, { parseJSON: true });
}); });
}, },

View File

@ -4,6 +4,41 @@
var lastCall = null; 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) { function get(url, callback, options) {
options = options || { options = options || {
method: 'GET', method: 'GET',
@ -52,6 +87,7 @@
torque.net = { torque.net = {
get: get, get: get,
post: post, post: post,
jsonp: jsonp,
lastCall: function() { return lastCall; } lastCall: function() { return lastCall; }
}; };

30
test/request.js Normal file
View File

@ -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);
});

View File

@ -19,5 +19,6 @@
<script src="core.js"></script> <script src="core.js"></script>
<script src="provider.jsonarray.js"></script> <script src="provider.jsonarray.js"></script>
<script src="provider.json.js"></script> <script src="provider.json.js"></script>
<script src="request.js"></script>
</body> </body>
</html> </html>