137 lines
3.5 KiB
JavaScript
137 lines
3.5 KiB
JavaScript
/**
|
|
*
|
|
* User: Martin Donnelly
|
|
* Date: 2016-09-09
|
|
* Time: 15:28
|
|
*
|
|
*/
|
|
'use strict';
|
|
/**
|
|
*
|
|
* User: Martin Donnelly
|
|
* Date: 2016-07-27
|
|
* Time: 09:23
|
|
*
|
|
*/
|
|
|
|
var WeatherModel = Backbone.Model.extend({
|
|
initialize: function() {
|
|
this.set('url','https://api.darksky.net/forecast/9ad2a41d420f3cf4960571bb886f710c/' + 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();
|
|
var now = new Date;
|
|
var mod = 1800000 - (now.getTime() % 1800000);
|
|
var weatherTrigger = function() {
|
|
this.update();
|
|
};
|
|
|
|
setTimeout(weatherTrigger.bind(this), mod + 10);
|
|
},
|
|
getWeather: function() {
|
|
var 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) {
|
|
var stored = {
|
|
temperature: data.currently.temperature,
|
|
icon: data.currently.icon,
|
|
summary: data.currently.summary
|
|
};
|
|
self.set('data',stored);
|
|
},
|
|
error: function(xhr, type) {
|
|
console.error('ajax error');
|
|
console.error(xhr);
|
|
console.error(type);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
var ClockModel = Backbone.Model.extend({
|
|
initialize: function() {
|
|
this.set('now',new Date);
|
|
this.update();
|
|
},
|
|
update: function() {
|
|
var now = new Date;
|
|
var mod = 60000 - (now.getTime() % 60000);
|
|
this.set('now',now);
|
|
|
|
var clockFn = function() {
|
|
this.update();
|
|
};
|
|
|
|
setTimeout(clockFn.bind(this), mod + 10);
|
|
}
|
|
});
|
|
|
|
var 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() {
|
|
var now = this.model.get('now');
|
|
//var curTime = now.format('<span class="hour">{24hr}</span>{mm}');
|
|
var curTime = now.format('<span class="time">{24hr} {mm}</span>');
|
|
|
|
var curDate = now.format('{yyyy}-{MM}-{dd}');
|
|
if (this.prevTime !== curTime) {
|
|
this.$time.html(curTime);
|
|
this.prevTime = curTime;
|
|
}
|
|
|
|
if (this.prevDate !== curDate) {
|
|
this.$date.html(now.format(
|
|
'<span class="wd-{do}">{Weekday}</span> <span class="mo mo-{M}">{Month} {dd}</span> {yyyy}'));
|
|
this.prevDate = curDate;
|
|
}
|
|
}
|
|
});
|
|
|
|
var Weather = Backbone.View.extend({
|
|
tagName: 'div',
|
|
initialize: function() {
|
|
_.bindAll(this, 'render');
|
|
this.model.bind('change', this.render);
|
|
this.$weatherText = $('#weatherDescription');
|
|
this.$weatherTemp = $('#temp');
|
|
this.$weatherIcon = $('#weatherIcon');
|
|
},
|
|
render: function() {
|
|
console.log('Weather:Render');
|
|
var data = this.model.get('data');
|
|
var ws = '<i class="wi wi-forecast-io-' + data.icon + '"></i>';
|
|
|
|
this.$weatherTemp.empty().html(parseInt(data.temperature) + '°c ');
|
|
this.$weatherText.empty().html(data.summary);
|
|
|
|
this.$weatherIcon.empty().html(ws);
|
|
|
|
|
|
|
|
console.log(data);
|
|
}
|
|
|
|
});
|