From 51238e057bd944dd83cada33c214b297dca78095 Mon Sep 17 00:00:00 2001 From: Torsten Meissner Date: Thu, 28 Mar 2013 23:51:08 +0100 Subject: [PATCH] moved ajaxCall() & getHTTPObject() into seperate javascript library 'gcf.js' --- index.html | 1 + scripts/gcf.js | 69 +++++++++++++++++++++++++++++++++++++++++ scripts/haltestellen.js | 62 ++---------------------------------- 3 files changed, 72 insertions(+), 60 deletions(-) create mode 100644 scripts/gcf.js diff --git a/index.html b/index.html index 32b1a50..cdb9894 100644 --- a/index.html +++ b/index.html @@ -21,6 +21,7 @@
+ diff --git a/scripts/gcf.js b/scripts/gcf.js new file mode 100644 index 0000000..c031d68 --- /dev/null +++ b/scripts/gcf.js @@ -0,0 +1,69 @@ +/** + * Created with JetBrains WebStorm. + * User: torsten + * Date: 28.03.13 + * Time: 23:07 + * To change this template use File | Settings | File Templates. + */ + +gcf = { + + // xmlhttp object function + getHTTPObject : function () { + + // debug log + if (DEBUG === 1) {console.log("xml http object function");} + + // variable definitions + var xhr; + + // check for availibility if xmlhttprequest object + if(window.XMLHttpRequest) { + xhr = new XMLHttpRequest(); + } else if(window.ActiveXObject) { + xhr = new ActiveXObject("Msxml2.XMLHTTP"); + } + + return xhr; + + }, + + + // ajax call function + ajaxCall : function (dataUrl, outputElement, callback, responseType) { + + // debug log + if (DEBUG === 1) {console.log("ajax function");} + + // variable definitions + var response, + request = this.getHTTPObject(); // get the xmlhttp object which is supported + + outputElement.innerHTML = "Lade Daten ..."; + + request.onreadystatechange = function() { + + if(request.readyState === 4 && request.status === 200) { + + //save ajax response + if (responseType === "json") { + response = JSON.parse(request.responseText); + } else if (responseType === "xml") { + response = request.responseXML; + } else { + response = request.responseText; + } + + // check if callback is a function + if(typeof callback === "function") { + callback(response); + } + } + }; + + request.open("get", dataUrl, true); + request.send(null); + + } + +} \ No newline at end of file diff --git a/scripts/haltestellen.js b/scripts/haltestellen.js index 18a389f..cc7d817 100644 --- a/scripts/haltestellen.js +++ b/scripts/haltestellen.js @@ -3,64 +3,6 @@ var DEBUG; -// xmlhttp object function -function getHTTPObject() { - - // debug log - if (DEBUG === 1) {console.log("xml http object function");} - - // variable definitions - var xhr; - - // check for availibility if xmlhttprequest object - if(window.XMLHttpRequest) { - xhr = new XMLHttpRequest(); - } else if(window.ActiveXObject) { - xhr = new ActiveXObject("Msxml2.XMLHTTP"); - } - - return xhr; - -} - - -// ajax call function -function ajaxCall(dataUrl, outputElement, callback, responseType) { - - // debug log - if (DEBUG === 1) {console.log("ajax function");} - - // variable definitions - var response, - request = getHTTPObject(); // get the xmlhttp object which is supported - - outputElement.innerHTML = "Lade Daten ..."; - - request.onreadystatechange = function() { - - if(request.readyState === 4 && request.status === 200) { - - //save ajax response - if (responseType === "json") { - response = JSON.parse(request.responseText); - } else if (responseType === "xml") { - response = request.responseXML; - } else { - response = request.responseText; - } - - // check if callback is a function - if(typeof callback === "function") { - callback(response); - } - } - }; - - request.open("get", dataUrl, true); - request.send(null); - -} - // wrap all in anonymous function to get out of global scope (function() { @@ -102,7 +44,7 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?query=abfahrten.do&ort=dresden&hst=" + hstName); // get the data from the server with an ajax call - ajaxCall(hstUrl, target, haltestelle.processAbfahrten, "text"); + gcf.ajaxCall(hstUrl, target, this.processAbfahrten, "text"); }, @@ -188,7 +130,7 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?query=haltestelle.do&ort=dresden&hst=" + hstName); // get the data from the server with an ajax call - ajaxCall(hstUrl, target, haltestelle.processHaltestellen, "text"); + gcf.ajaxCall(hstUrl, target, this.processHaltestellen, "text"); },