/** * * User: Martin Donnelly * Date: 2016-10-03 * Time: 14:20 * */ let TrainModel = Backbone.Model.extend({ initialize: function () { let fromStation = this.get('from'); let toStation = this.get('to'); let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; let routeUrl = '/gettrains?from=' + fromStation + '&to=' + toStation; let target = fromStation + toStation; this.set('url', url); this.set('routeUrl', routeUrl); this.set('target', target); this.set('visible', false); this.update(); }, update: function () { this.getTrain(); const now = new Date; const mod = 60000 - (now.getTime() % 60000); let trainUpdateFn = function () { this.update(); }; setTimeout(trainUpdateFn.bind(this), mod + 10); }, getTrain: function () { let url = this.get('url'); let self = this; $.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); } }); }, getRoute: function () { let url = this.get('routeUrl'); let self = this; if (this.get('visible') === true) { this.set('visible', false); } else { $.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) { // getTrainsCB(data); //console.log('Got', data); self.set('route', data); self.set('visible', true); }, error: function (xhr, type) { console.error('ajax error'); console.error(xhr); console.error(type); } }); } } }); let TrainView = Backbone.View.extend({ tagName: 'div', initialize: function () { _.bindAll(this, 'render'); this.model.bind('change', this.render); this.$trains = $('#trains'); this.$traininfo = $('#traininfo'); this.$traintext = $('#trainResults'); this.$el = this.$trains; this.initView(); }, events: { 'click': 'showTrains' }, render: function () { let output, status; let obj = this.model.get('trainData'); let visible = this.model.get('visible'); let route = this.model.get('route'); output = (obj.eta.toLowerCase() === 'on time') ? obj.sta : obj.eta; status = (obj.eta.toLowerCase() === 'on time') ? 'ontime' : 'delayed'; this.$button.html(output); this.$button.removeClass('delayed').removeClass('ontime').addClass(status); if (visible) { let ws = `
Destination | Time | Status | Platform |
---|---|---|---|
${dest.locationName} ${via} | ${time} | ${status} | ${platform} |
${dest.locationName} ${via} | ${time} | ❌ ${item.cancelReason} | |
🚌 ${dest.locationName} ${via} | ${time} | ${status} | ${platform} |