westdc-zf1/htdocs/js/timemap/loaders/json.js

121 lines
3.7 KiB
JavaScript
Raw Normal View History

2009-12-20 10:56:42 +00:00
/*
2010-06-29 08:58:09 +00:00
* Timemap.js Copyright 2010 Nick Rabinowitz.
2009-12-20 10:56:42 +00:00
* Licensed under the MIT License (see LICENSE.txt)
*/
/**
* @fileOverview
* JSON Loaders (JSONP, JSON String)
*
* @author Nick Rabinowitz (www.nickrabinowitz.com)
*/
2010-06-29 08:58:09 +00:00
// for JSLint
/*global TimeMap */
2009-12-20 10:56:42 +00:00
/**
* @class
2010-06-29 08:58:09 +00:00
* JSONP loader - expects a service that takes a callback function name as
2009-12-20 10:56:42 +00:00
* the last URL parameter.
*
* <p>The jsonp loader assumes that the JSON can be loaded from a url to which a
* callback function name can be appended, e.g. "http://www.test.com/getsomejson.php?callback="
* The loader then appends a nonce function name which the JSON should include.
* This works for services like Google Spreadsheets, etc., and accepts remote URLs.</p>
2010-06-29 08:58:09 +00:00
*
* @augments TimeMap.loaders.remote
2009-12-20 10:56:42 +00:00
*
2010-06-29 08:58:09 +00:00
* @example
TimeMap.init({
2009-12-20 10:56:42 +00:00
datasets: [
{
title: "JSONP Dataset",
type: "jsonp",
options: {
2010-06-29 08:58:09 +00:00
url: "http://www.example.com/getsomejson.php?callback="
2009-12-20 10:56:42 +00:00
}
}
2010-06-29 08:58:09 +00:00
],
// etc...
});
2009-12-20 10:56:42 +00:00
*
* @constructor
2010-06-29 08:58:09 +00:00
* @param {Object} options All options for the loader:
* @param {String} options.url URL of JSON service to load, callback name left off
* @param {mixed} [options[...]] Other options (see {@link TimeMap.loaders.remote})
2009-12-20 10:56:42 +00:00
*/
TimeMap.loaders.jsonp = function(options) {
2010-06-29 08:58:09 +00:00
var loader = new TimeMap.loaders.remote(options);
/**
* JSONP load function. Creates a callback function and adds a script tag
* with the appropriate URL to the document, triggering the HTTP request.
* @name TimeMap.loaders.jsonp#load
* @function
*
* @param {TimeMapDataset} dataset Dataset to load data into
* @param {Function} callback Function to call once data is loaded
*/
loader.load = function(dataset, callback) {
// get loader callback name
var callbackName = this.getCallbackName(dataset, callback),
// create a script tag
script = document.createElement("script");
// set the src attribute and add to the document
script.src = this.url + "TimeMap.loaders.cb." + callbackName;
document.body.appendChild(script);
};
return loader;
2009-12-20 10:56:42 +00:00
};
/**
* @class
* JSON string loader factory - expects a plain JSON array.
*
* <p>The json_string loader assumes an array of items in plain JSON, with no
* callback function - this will require a local URL.</p>
2010-06-29 08:58:09 +00:00
* <p>Note that this loader requires lib/json2.pack.js.</p>
*
* @augments TimeMap.loaders.remote
2009-12-20 10:56:42 +00:00
*
2010-06-29 08:58:09 +00:00
* @requires lib/json2.pack.js
2009-12-20 10:56:42 +00:00
*
2010-06-29 08:58:09 +00:00
* @example
TimeMap.init({
2009-12-20 10:56:42 +00:00
datasets: [
{
title: "JSON String Dataset",
type: "json_string",
options: {
url: "mydata.json" // Must be a local URL
}
}
2010-06-29 08:58:09 +00:00
],
// etc...
});
2009-12-20 10:56:42 +00:00
*
2010-06-29 08:58:09 +00:00
* @param {Object} options All options for the loader
* @param {String} options.url URL of JSON file to load
* @param {mixed} [options[...]] Other options (see {@link TimeMap.loaders.remote})
2009-12-20 10:56:42 +00:00
*/
TimeMap.loaders.json_string = function(options) {
var loader = new TimeMap.loaders.remote(options);
2010-06-29 08:58:09 +00:00
/**
* Parse a JSON string into a JavaScript object, using the json2.js library.
* @name TimeMap.loaders.json_string#parse
* @function
* @param {String} json JSON string to parse
* @returns {Object} Parsed JavaScript object
*/
2009-12-20 10:56:42 +00:00
loader.parse = JSON.parse;
2010-06-29 08:58:09 +00:00
2009-12-20 10:56:42 +00:00
return loader;
2010-06-29 08:58:09 +00:00
};
2009-12-20 10:56:42 +00:00
// Probably the default json loader should be json_string, not
// jsonp. I may change this in the future, so I'd encourage you to use
// the specific one you want.
TimeMap.loaders.json = TimeMap.loaders.jsonp;