99 lines
2.6 KiB
JavaScript
99 lines
2.6 KiB
JavaScript
/**
|
|
*
|
|
* User: Martin Donnelly
|
|
* Date: 2016-10-03
|
|
* Time: 14:20
|
|
*
|
|
*/
|
|
|
|
const WeatherModel = Backbone.Model.extend({
|
|
'initialize': function() {
|
|
const 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();
|
|
const now = new Date;
|
|
const mod = 1800000 - (now.getTime() % 1800000);
|
|
const weatherTrigger = () => {
|
|
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,
|
|
'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);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
const 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');
|
|
|
|
const 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);
|
|
}
|
|
|
|
});
|
|
|
|
const WeatherSlim = Backbone.View.extend({
|
|
'tagName': 'div',
|
|
'initialize': function() {
|
|
_.bindAll(this, 'render');
|
|
this.model.bind('change', this.render);
|
|
this.$weather = $('#weather');
|
|
this.render();
|
|
},
|
|
'render': function() {
|
|
const summary = this.model.get('summary');
|
|
const temp = this.model.get('temperature');
|
|
const daily = this.model.get('daily');
|
|
|
|
const ws = `${summary} ${temp}° <em>${daily}</em>`;
|
|
|
|
this.$weather.empty().html(ws);
|
|
}
|
|
|
|
});
|