silvrgit/app/js/modules/fx.js
2017-09-06 08:42:13 +01:00

76 lines
1.8 KiB
JavaScript

/**
* Created by mdonnel on 22/03/2017.
*/
const FxModel = Backbone.Model.extend({
'initialize': function () {
this.set('url', '/fx');
this.set('fxdata', {});
this.update();
},
'update': function () {
this.getFX();
const now = new Date;
const mod = 900000 - (now.getTime() % 900000);
const fxUpdateFn = function() {
this.update();
};
setTimeout(fxUpdateFn.bind(this), mod + 10);
},
'getFX': function() {
const url = this.get('url');
const self = this;
$.ajax({
'type': 'GET',
'url': url,
'data': '',
'dataType': 'json',
'timeout': 10000,
// 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) {
let fxdata = {};
if (data.rates !== undefined) {
const gpbex = (1 / data.rates.GBP);
const sekex = (gpbex * data.rates.SEK);
fxdata = {
'usd': 1,
'gbp': data.rates.GBP,
'sek': data.rates.SEK,
'gpbe': gpbex,
'sekex': sekex
};
}
self.set('fxdata', fxdata);
},
'error': function(xhr, type) {
console.log('ajax error');
console.log(xhr);
console.log(type);
}
});
}
});
const FxView = Backbone.View.extend({
'tagName': 'div',
'initialize': function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$fx = $('#fx');
},
'render': function () {
const fxdata = this.model.get('fxdata');
this.$fx.html(`£1 = $${parseFloat(fxdata.gpbe.toFixed(2))} = ${ parseFloat(fxdata.sekex.toFixed(2))} SEK`);
}
});