73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
|
/**
|
||
|
* Created by mdonnel on 22/03/2017.
|
||
|
*/
|
||
|
|
||
|
let 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);
|
||
|
|
||
|
let fxUpdateFn = function() {
|
||
|
this.update();
|
||
|
};
|
||
|
setTimeout(fxUpdateFn.bind(this), mod + 10);
|
||
|
},
|
||
|
getFX: function() {
|
||
|
let url = this.get('url');
|
||
|
let 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 gpbex = (1 / data.rates.GBP);
|
||
|
let sekex = (gpbex * data.rates.SEK);
|
||
|
let 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);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
let FxView = Backbone.View.extend({
|
||
|
tagName: 'div',
|
||
|
initialize: function () {
|
||
|
_.bindAll(this, 'render');
|
||
|
this.model.bind('change', this.render);
|
||
|
this.$fx = $('#fx');
|
||
|
},
|
||
|
render: function () {
|
||
|
let fxdata = this.model.get('fxdata');
|
||
|
this.$fx.html(`£1 = $${parseFloat(fxdata.gpbe.toFixed(2))} = ${ parseFloat(fxdata.sekex.toFixed(2))} SEK`);
|
||
|
}
|
||
|
});
|