'use strict'; /** * * User: Martin Donnelly * Date: 2016-07-27 * Time: 09:23 * */ const WeatherModel = Backbone.Model.extend({ 'initialize': function() { this.set('url', `https://api.forecast.io/forecast/0657dc0d81c037cbc89ca88e383b6bbf/${ this.get('lat').toString() },${ this.get('long').toString() }?units=uk2&exclude=minutely,hourly,daily,alerts,flags`); console.log(this.get('url')); this.update(); }, 'update': function() { this.getWeather(); const now = new Date; const mod = 1800000 - (now.getTime() % 1800000); const weatherTrigger = function () { this.update(); }; setTimeout(weatherTrigger.bind(this), mod + 10); }, 'getWeather': function() { const self = this; $.ajax({ 'type': 'GET', 'url': self.get('url'), 'data': '', 'dataType': 'jsonp', 'timeout': 10000, 'context': $('body'), '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) { const stored = { 'temperature': data.currently.temperature, 'icon': data.currently.icon }; self.set('data', stored); }, 'error': function(xhr, type) { console.error('ajax error'); console.error(xhr); console.error(type); } }); } }); const ClockModel = Backbone.Model.extend({ 'initialize': function() { const now = new Sugar.Date(); this.set('now', now); this.update(); }, 'update': function() { const now = new Sugar.Date(); const mod = 60000 - (now.getTime().raw % 60000); this.set('now', now); const clockFn = function () { this.update(); }; setTimeout(clockFn.bind(this), mod + 10); } }); const Clock = Backbone.View.extend({ 'tagName': 'div', 'initialize': function() { _.bindAll(this, 'render'); this.model.bind('change', this.render); this.$date = $('#date'); this.$time = $('#time'); this.render(); }, 'render': function() { const now = this.model.get('now'); console.log('now', now); const curTime = now.format('{24hr}{mm}').raw; const curDate = now.format('{yyyy}-{MM}-{dd}').raw; console.log('curTime', curTime); console.log('curDate', curDate); if (this.prevTime !== curTime) { this.$time.html(curTime); this.prevTime = curTime; } if (this.prevDate !== curDate) { this.$date.html(now.format( '{Weekday}
{Month} {dd}
{yyyy}')); this.prevDate = curDate; } } }); const Weather = Backbone.View.extend({ 'tagName': 'div', 'initialize': function() { _.bindAll(this, 'render'); this.model.bind('change', this.render); this.$weatherText = $('#weatherText'); }, 'render': function() { console.log('Weather:Render'); const data = this.model.get('data'); this.$weatherText.html(`${parseInt(data.temperature) }°c `); skycons.remove('icon1'); skycons.add('icon1', data.icon); } });