我应该将json结果放在money.js的什么地方? [英] Where should I put the json result in money.js?

查看:99
本文介绍了我应该将json结果放在money.js的什么地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读文档 http://openexchangerates.github.io/money.js/#fx.rates 它说您需要设置费率:

Reading the documentation http://openexchangerates.github.io/money.js/#fx.rates Its says you need to set up your rates:

 fx.base = "USD";
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 */
}

我完全理解,只有这些才是静态汇率.它说要具有动态速率,您需要添加json api:

Which I totally get, only these will then be static rates. It says to have dynamic rates you need to add the json api:

// Load exchange rates data via AJAX:
    $.getJSON(
      // NB: using Open Exchange Rates here, but you can use any source!
      'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
          }
        }
      });

但是,当我这样做时,欧元汇率没有变化,仍为0.74.费率不会改变或调整.

But when I do this the EUR rate doens't change, its still 0.74. The rates don't change or adjust.

在money.js脚本之前或之后,我将json请求放在哪里? 还是在money.js文件中?如果在money.js文件中,则位于底部还是顶部? -或在这里告诉我我要去哪里错了

Where do I put the json request, before or after the money.js script? or inside the money.js file? if inside the money.js file, where, at the bottom or top? - or please advise where I am going wrong here

推荐答案

在money.js加载后,您应该可以在任何地方重载此代码(JavaScript通常允许这样做).

You should be able to overload this anywhere once money.js has loaded (javascript allows this in general).

没有足够的细节来明确地说,但是我猜这是一种常见的竞争条件,因为网络调用是异步的,所以如果您正在执行以下操作:

There isn't enough detail to say definitively, but my guess is this is a common race condition because web calls are asynchronous, so if you are doing something like this:

-Load Money js
-Call Web call for rates
-Use money.js

当您使用money.js时,您的费率电话可能尚未返回,因此,当您调用它时,您正在使用默认值.如果这是您的问题,则需要在实际设置费率时将代码放入回调中,如下所示:

It's likely that when you use money.js, your rate call hasn't returned yet, so when you call it you are using the default values. If this is your issue, you need to put your code in the callback for when you actually set your rates, like so:

$.getJSON(
    // NB: using Open Exchange Rates here, but you can use any source!
    'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
        }
    }
    // YOUR CODE HERE
  });

文档实际上提到了这一点:

The documentation actually mentions this:

You'll need to wait until the AJAX request has completed before you can
begin processing conversions. You may also wish to cache
proximate/historical rates on your server and bootstrap them inline
into the HTML as a backup.

这篇关于我应该将json结果放在money.js的什么地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆