100 lines
2.8 KiB
JavaScript
100 lines
2.8 KiB
JavaScript
/**
|
|
*
|
|
* User: Martin Donnelly
|
|
* Date: 2016-10-03
|
|
* Time: 14:20
|
|
*
|
|
*/
|
|
|
|
let WeatherModel = Backbone.Model.extend({
|
|
initialize: function() {
|
|
let geo = this.get('geo');
|
|
this.set('url','https://api.darksky.net/forecast/9ad2a41d420f3cf4960571bb886f710c/' + geo.coords.latitude.toString() + ',' + geo.coords.longitude.toString() + '?units=uk2&exclude=minutely,hourly,alerts,flags');
|
|
this.update();
|
|
},
|
|
update: function() {
|
|
this.getWeather();
|
|
let now = new Date;
|
|
let mod = 1800000 - (now.getTime() % 1800000);
|
|
let weatherTrigger = function() {
|
|
this.update();
|
|
};
|
|
|
|
setTimeout(weatherTrigger.bind(this), mod + 10);
|
|
},
|
|
getWeather: function() {
|
|
let 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,
|
|
daily: data.daily.summary
|
|
};
|
|
self.set(stored);
|
|
},
|
|
error: function(xhr, type) {
|
|
console.error('ajax error');
|
|
console.error(xhr);
|
|
console.error(type);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
let 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 ws = '<i class="wi wi-forecast-io-' + this.model.get('icon') + '"></i>';
|
|
|
|
this.$weatherTemp.empty().html(parseInt(this.model.get('temperature')) + '°c ');
|
|
this.$weatherText.empty().html(this.model.get('summary'));
|
|
|
|
this.$weatherIcon.empty().html(ws);
|
|
}
|
|
|
|
});
|
|
|
|
let WeatherSlim = Backbone.View.extend({
|
|
tagName: 'div',
|
|
initialize: function() {
|
|
_.bindAll(this, 'render');
|
|
this.model.bind('change', this.render);
|
|
this.$weather = $('#weather');
|
|
this.render();
|
|
},
|
|
render: function() {
|
|
|
|
let summary = this.model.get('summary');
|
|
let temp = this.model.get('temperature');
|
|
let daily = this.model.get('daily');
|
|
|
|
let ws = `${summary} ${temp}° <em>${daily}</em>`;
|
|
|
|
this.$weather.empty().html(ws);
|
|
}
|
|
|
|
});
|