javascript - Money.js + LocalStorage rates -
i'm using intel xdk make conversion tool, script detects if device has internet connection, if yes use latest rate openexchangerates via json , storage in localstorage:
$.getjson('https://openexchangerates.org/api/latest.json?app_id=xxxxxxxxxxxxxxxxxxxxxxxxxx',function(data) { var localdata = json.stringify(data); localstorage.setitem('convrates', localdata); // check money.js has finished loading: if ( typeof fx !== "undefined" && fx.rates ) { fx.rates = data.rates; fx.base = data.base; } else { // if not, apply fxsetup global: var fxsetup = { rates : data.rates, base : data.base } } });
this ok! when try info localstorage, nothing happens...
var localdata = json.parse(localstorage.getitem('convrates')); $.getjson(localdata,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 fxsetup global: var fxsetup = { rates : data.rates, base : data.base } } });
this json data exemple:
{ "disclaimer": "exchange rates provided informational purposes only, , not constitute financial advice of kind. although every attempt made ensure quality, no guarantees given whatsoever of accuracy, validity, availability, or fitness purpose - please use @ own risk. usage subject acceptance of terms , conditions of service, available at: https://openexchangerates.org/terms/", "license": "data sourced various providers public-facing apis; copyright may apply; resale prohibited; no warranties given of kind. bitcoin data provided http://coindesk.com. usage subject acceptance of license agreement available at: https://openexchangerates.org/license/", "timestamp": 1417507252, "base": "usd", "rates": { "aed": 3.673268, "afn": 57.871426, "all": 112.5408, "amd": 439.297503, "ang": 1.7891, "aoa": 101.106125, "ars": 8.531535, "aud": 1.174523, "awg": 1.79, "azn": 0.783933, "bam": 1.570651, "bbd": 2, "bdt": 77.73667, } }
i don't know how make it.
you can treat localstorage kind of normal js object, first off don't need stringify , parse js object. instead of writing
var localdata = json.stringify(data); localstorage.setitem('convrates', localdata);
you can do
localstorage['convrates'] = data;
or even
localstorage.convrates = data;
to retrieve data, may still want use getitem()
, return null rather throw error if convrates
doesn't exist, retrieve data again do
data = localstorage.getitem('convrates');
which leave data
either null
or convrates
object stored there.
calling $.getjson
on localdata
doesn't make sense, that's js object, not url.
Comments
Post a Comment