diff --git a/app/app.js b/app/app.js index d625a4f..62c5d2f 100644 --- a/app/app.js +++ b/app/app.js @@ -96,134 +96,19 @@ }); }; - let updateBTC = function(g, u) { - let title, total, elm = $('#btc'); - if (lastGBP !== 0) { - elm.removeClass(); - if (g > lastGBP) { - elm.addClass('up'); - } else if (g < lastGBP) { - elm.addClass('down'); - } - } else { - lows.gbp = g; - lows.usd = u; - - highs.gbp = g; - highs.usd = u; - } - - lastGBP = g; - lastUSD = u; - - if (g < lows.gbp) lows.gbp = g; - if (u < lows.usd) lows.usd = u; - - if (highs.gbp < g) highs.gbp = g; - if (highs.usd < u) highs.usd = u; - - total = myBTC * g; - - title = 'High: $' + parseFloat(highs.usd.toFixed(2)) + ' / Low $' + parseFloat(lows.usd.toFixed(2)); - elm.html('$' + parseFloat(u.toFixed(2)) + ' / £' + parseFloat(g.toFixed(2)) + ' (£' + parseFloat(total.toFixed(2)) + ')'); - elm.prop('title', title); - }; - - let updateFX = function(data) { - let elm = $('#fx'); - elm.html('£1 = $' + parseFloat(data.gpbe.toFixed(2)) + ' = ' + parseFloat(data.sekex.toFixed(2)) + ' SEK'); - }; - - this.bind('updateFX', function(data) { - $('#fx').html('£1 = $' + parseFloat(data.gpbe.toFixed(2)) + ' = ' + parseFloat(data.sekex.toFixed(2)) + ' SEK'); - }); - let btcValue = function() { - let url = '/btc'; - $.ajax({ - type: 'GET', - url: url, - data: '', - dataType: 'json', - timeout: 10000, - //contentType: ('application/json'), - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type' - }, - success: function(data) { - // console.log(data); - let gbp = data.bpi.GBP.rate_float, - usd = data.bpi.USD.rate_float; - - updateBTC(gbp, usd); - }, - error: function(xhr, type) { - console.log('ajax error'); - console.log(xhr); - console.log(type); - } - }); - }; - - /*this.bind('getBTC', function() { - btcValue(); - });*/ - - let getFX = function() { - let url = '/fx'; - - $.ajax({ - type: 'GET', - url: url, - data: '', - dataType: 'json', - - timeout: 10000, - - //contentType: ('application/json'), - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type' - - }, - success: function(data) { - let gpbex = (1 / data.rates.GBP); - let sekex = (gpbex * data.rates.SEK); - let fxdata = { - usd: 1, - gbp: data.rates.GBP, - sek: data.rates.SEK, - gpbe: gpbex, - sekex: sekex - }; - self.trigger('updateFX', fxdata); - }, - error: function(xhr, type) { - console.log('ajax error'); - console.log(xhr); - console.log(type); - } - }); - }; - - this.bind('getFX', function() { - getFX(); - }); let getNextTrainTime = function(toStation, fromStation) { let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; let target = fromStation + toStation; - $.ajax({ - type: 'GET', - url: url, + $.ajax({ 'GET', + u + type:rl: url, data: '', dataType: 'json', @@ -332,21 +217,19 @@ tick(); get_weather(); - self.trigger('getBTC'); - self.trigger('getFX'); getNextTrainTime('dbe', 'glq'); getNextTrainTime('glq', 'dbe'); // start 15 minute timer _fastTimer = setInterval(function() { - self.trigger('getBTC'); + getNextTrainTime('dbe', 'glq'); getNextTrainTime('glq', 'dbe'); }, (60000)); _slowTimer = setInterval(function() { - self.trigger('getFX'); + get_weather(); }, (60000 * 15)); diff --git a/app/js/modules/fx.js b/app/js/modules/fx.js new file mode 100644 index 0000000..8ed3647 --- /dev/null +++ b/app/js/modules/fx.js @@ -0,0 +1,72 @@ +/** + * Created by mdonnel on 22/03/2017. + */ + +let FxModel = Backbone.Model.extend({ + initialize: function () { + this.set('url', '/fx'); + this.set('fxdata', {}); + this.update(); + }, + update: function () { + this.getFX(); + const now = new Date; + const mod = 900000 - (now.getTime() % 900000); + + let fxUpdateFn = function() { + this.update(); + }; + setTimeout(fxUpdateFn.bind(this), mod + 10); + }, + getFX: function() { + let url = this.get('url'); + let self = this; + $.ajax({ + type: 'GET', + url: url, + data: '', + dataType: 'json', + + timeout: 10000, + + //contentType: ('application/json'), + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type' + + }, + success: function(data) { + let gpbex = (1 / data.rates.GBP); + let sekex = (gpbex * data.rates.SEK); + let fxdata = { + usd: 1, + gbp: data.rates.GBP, + sek: data.rates.SEK, + gpbe: gpbex, + sekex: sekex + }; + self.set('fxdata', fxdata); + }, + error: function(xhr, type) { + console.log('ajax error'); + console.log(xhr); + console.log(type); + } + }); + } +}); + + +let FxView = Backbone.View.extend({ + tagName: 'div', + initialize: function () { + _.bindAll(this, 'render'); + this.model.bind('change', this.render); + this.$fx = $('#fx'); + }, + render: function () { + let fxdata = this.model.get('fxdata'); + this.$fx.html(`£1 = $${parseFloat(fxdata.gpbe.toFixed(2))} = ${ parseFloat(fxdata.sekex.toFixed(2))} SEK`); + } +}); diff --git a/app/js/modules/train.js b/app/js/modules/train.js index 630b05c..b3597f1 100644 --- a/app/js/modules/train.js +++ b/app/js/modules/train.js @@ -6,9 +6,48 @@ * */ -var TrainModel = Backbone.Model.extend({}); +let TrainModel = Backbone.Model.extend({ + initialize: function() { + console.log(this.get('to')); + let fromStation = this.get('from'); + let toStation = this.get('to'); + let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; + let target = fromStation + toStation; + this.set('url', url); + this.set('target', target); + this.update(); + }, update: function() { + this.getTrain(); + }, + getTrain: function() { + let url = this.get('url'); + let self = this; + console.log(url); + $.ajax({ + type: 'GET', + url: url, + data: '', + dataType: 'json', + timeout: 10000, + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type' + }, + success: function(data) { + console.log(data); + self.set('trainData', data); + }, + error: function(xhr, type) { + console.log('ajax error'); + console.log(xhr); + console.log(type); + } + }); + } +}); -var Train = Backbone.View.extend({ +let Train = Backbone.View.extend({ tagName: 'div', initialize: function() { _.bindAll(this, 'render'); @@ -18,27 +57,21 @@ var Train = Backbone.View.extend({ }, render: function() { console.log('Train:Render'); - var ws =''; - var data = this.model.get('data'); + let ws = ''; + let data = this.model.get('data'); console.log(this.model); - if (data.length > 0) - { - for (var i = 0; i < data.length; i++) - { + if (data.length > 0) { + for (let i = 0; i < data.length; i++) { ws = ws + '