silvrgit/app/js/parts/clock.js
Martin Donnelly b945f54fd0 Clock
2016-10-05 15:08:57 +01:00

118 lines
3.1 KiB
JavaScript

'use strict';
/**
*
* User: Martin Donnelly
* Date: 2016-07-27
* Time: 09:23
*
*/
var 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();
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
};
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 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><br><span class="mo mo-{M}">{Month} {dd}</span><br>{yyyy}'));
this.prevDate = curDate;
}
}
});
var 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');
var data = this.model.get('data');
this.$weatherText.html(parseInt(data.temperature) + '&deg;c&nbsp;');
skycons.remove('icon1');
skycons.add('icon1', data.icon);
}
});