Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:17+00:00 2026-06-17T13:59:17+00:00

I am busy creating a currency Module for my web app, I am using

  • 0

I am busy creating a currency Module for my web app, I am using Yahoo Finance API to return the currency conversion rate of 2 currencies that I have defined in my local DB. I get the JSON data fine from the API, I just want to Bind the JSON data that I have received from the Finance API to my existing Viewmodel using ko.computed. I am not sure if I should be using ko.computed to achieve this, so any advice will help greatly.

Here is my code:

    var currency = function (data) {
        var self = this;
        self.CurrencyFrom = ko.observable(data.CurrencyFrom);
        self.CurrencyTo = ko.observable(data.CurrencyTo);
        self.ConversionRate = ko.computed(rates); // I WANT TO BIND THE VALUE FROM API HERE
    }

    var CurrencyModel = function (Currencies) {
        var self = this;
        self.Currencies = ko.observableArray(Currencies);

        self.AddCurrency = function () {
            self.Currencies.push({
                CurrencyFrom: "",
                CurrencyTo: "",
                ConversionRate: ""
            });
        };

        self.RemoveCurrency = function (Currency) {
            self.Currencies.remove(Currency);
        };

        self.Save = function (Form) {
            alert("Could Now Save: " + ko.utils.stringifyJson(self.Currencies));
        };

        $.ajax({
            url: "CurrencyConfiguration.aspx/GetConfiguredCurrencies",
            // Current Page, Method  
            data: '{}',
            // parameter map as JSON  
            type: "POST",
            // data has to be POSTed  
            contentType: "application/json; charset=utf-8",
            // posting JSON content      
            dataType: "JSON",
            // type of data is JSON (must be upper case!)  
            timeout: 10000,
            // AJAX timeout  
            success: function (Result) {
                var MappedCurrencies =
              $.map(Result.d,
       function (item) {
           getRate(item.CurrencyFrom, item.CurrencyTo);

           return new currency(item);
       }
       );
                self.Currencies(MappedCurrencies);

            },
            error: function (xhr, status) {
                alert(status + " - " + xhr.responseText);
            }
        });
    };

    //3rd Party JSON result from Yahoo Finance API
    function getRate(from, to) {
        var script = document.createElement('script');
        script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=rates"); //HERE I CALL THE VALUE TO OBTAIN THE CONVERSION RATE FROM API
        document.body.appendChild(script);
    }


    //I WANT TO ADD THIS TO MY VIEWMODEL
    var rates = function parseExchangeRate(YahooData) {
                var rate = YahooData.query.results.row.rate;
        }


    $(document).ready(function () {
        var VM = new CurrencyModel();
        ko.applyBindings(VM);
        $('[rel=tooltip]').tooltip();
    })

The JSON returned from parseExchangeRate Function (Yahoo Query Result):

parseExchangeRate({"query":{"count":1,"created":"2013-01-18T06:46:41Z","lang":"en-US","results":{"row":{"rate":"0.1129","name":"ZAR to USD"}}}});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:59:18+00:00Added an answer on June 17, 2026 at 1:59 pm

    I see you’re already using jquery so I’ll use jquery for the JSONP too. I’m using a simple ko.observable for the conversion rate, and added a “bare” ko.computed that does the ajax request and asynchronously updates the observable. Let me know if you need any further clarification.

    JSFiddle example (with mock data instead of your initial ajax): http://jsfiddle.net/VsW5H/1/

    Updated code:

    var currency = function (data) {
      var self = this;
      self.CurrencyFrom = ko.observable(data.CurrencyFrom);
      self.CurrencyTo = ko.observable(data.CurrencyTo);
      self.ConversionRate = ko.observable(data.ConversionRate);
    
      ko.computed(function () {
        var from = self.CurrencyFrom(),
          to = self.CurrencyTo();
    
        if (!from || !to) {
          self.ConversionRate("N/A");
          return;
        }
    
        getRate(from, to).done(function (YahooData) {
          console.log("got yahoo data for [" + from + "," + to + "]: ", YahooData);
          self.ConversionRate(YahooData.query.results.row.rate);
        });
      });
    }
    
    var CurrencyModel = function (Currencies) {
      var self = this;
      self.Currencies = ko.observableArray(Currencies);
    
      self.AddCurrency = function () {
        self.Currencies.push(new currency({
          CurrencyFrom: "",
          CurrencyTo: "",
          ConversionRate: ""
        }));
      };
    
      self.RemoveCurrency = function (Currency) {
        self.Currencies.remove(Currency);
      };
    
      self.Save = function (Form) {
        alert("Could Now Save: " + ko.utils.stringifyJson(self.Currencies));
      };
    
      $.ajax({
        url: "CurrencyConfiguration.aspx/GetConfiguredCurrencies",
        // Current Page, Method  
        data: '{}',
        // parameter map as JSON  
        type: "POST",
        // data has to be POSTed  
        contentType: "application/json; charset=utf-8",
        // posting JSON content      
        dataType: "JSON",
        // type of data is JSON (must be upper case!)  
        timeout: 10000,
        // AJAX timeout  
        success: function (Result) {
          var MappedCurrencies = $.map(Result.d,
    
          function (item) {
            return new currency(item);
          });
          self.Currencies(MappedCurrencies);
        },
        error: function (xhr, status) {
          alert(status + " - " + xhr.responseText);
        }
      });
    
    };
    
    //3rd Party JSON result from Yahoo Finance API
    function getRate(from, to) {
      return $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=?");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm busy creating a very simplistic ruby on rails app that won't need a
I am busy creating a asp.net web site and using MS SQl 2008 as
I am creating an iOS app that displays a web view. How can I
I am busy creating a less complicated version of NinePatchDrawable that tiles the inner
I have been busy in my project creating a webapp (in struts) that manages
I have been busy creating an app for Red5. Imagine what was my surprise
I'm busy with creating a website that preferably needs to have a background picture
I am currently busy creating a search function that queries a user input string
I am busy writing an app that needs to be localized properly but the
I'm busy creating a metaclass that replaces a stub function on a class with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.