77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
/**
|
|
*
|
|
* User: Martin Donnelly
|
|
* Date: 2016-10-03
|
|
* Time: 14:20
|
|
*
|
|
*/
|
|
|
|
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();
|
|
};
|
|
},
|
|
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(stored);
|
|
},
|
|
error: function(xhr, type) {
|
|
console.error('ajax error');
|
|
console.error(xhr);
|
|
console.error(type);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
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 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);
|
|
}
|
|
|
|
});
|