openexchangerates / money.js

money.js is a tiny (1kb) javascript currency conversion library, for web & nodeJS
http://openexchangerates.github.io/money.js
MIT License
1.46k stars 127 forks source link

Adding the latest rates automatically #21

Open artworkjpm opened 9 years ago

artworkjpm commented 9 years ago

I can only get this to work if I put the rates in manually: fx.rates = { "EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR "GBP" : 0.647710, // etc... "HKD" : 7.781919, "USD" : 1, // always include the base rate (1:1) /* etc */ } What I really want is the API to auto update with the latest currency rates.

I wired up the json request: $.getJSON( // NB: using Open Exchange Rates here, but you can use any source! 'http://openexchangerates.org/api/latest.json?app_id=[I hid this]', function(data) { // Check money.js has finished loading: if (typeof fx !== "undefined" && fx.rates) { fx.rates = data.rates; fx.base = data.base; } else { // If not, apply to fxSetup global: var fxSetup = { rates: data.rates, base: data.base } } });

But it doesn't with latest currencies. Any ideas?

fmartingr commented 9 years ago

Can you specify the error that show up in your console?

I implemented moneyjs yesterday getting the rates from a json data source without problems, maybe your scripts are placed in incorrect order or you're not wrapping the getJSON call in a document ready event?