Second computed function causes error in Knockout code

woodfly

The Knockout code below applies tracks the values of the two textboxes using an observable and a computed observable.

Markup:

GBP:
<input type="textbox" data-bind="value: sendGbp" />
<br />
Exchange Rate: £1 GBP 
<br />
<input type="textbox" data-bind="value: sendFx" />

Javascript:

var transferItem = {
    receiveCurrencyCode : "JPY",
    exchangeRate : 123.223122
}

function viewModel(item) {

    var self = this;

    var initval = parseFloat(100).toFixed(2);
    self.sendGbp = ko.observable(initval);

    /** commented out error source
    self.labelFxRate = ko.computed(function() {
        return exchangeRate + " " + item.receiveCurrencyCode;
    });    
    **/

    self.sendFx = ko.computed({
        read: function () {
            return parseFloat(self.sendGbp() * item.exchangeRate).toFixed(2);
        },
        write: function (val) {
            var valGbp = parseFloat(val);
            self.sendGbp((valGbp / item.exchangeRate).toFixed(2));
        }
    });
}
ko.applyBindings(new viewModel(transferItem));

This works fine until I want to add another computed observable to display the

This involves going commenting out the section in the viewModel code and adding this a databound label to the markup:

Exchange Rate: £1 GBP  = <label data-bind="text: labelFxRate" />

But this causes an error and the markup fails to render completely

What's the correct way of applying the second computed variable to show additional properties from the transferItem object?

A full work in progress version is on jsfiddle: http://jsfiddle.net/WuvZD/3/

kon

It seems like you must not use self-closing tags for label. Also in your commented out code you used return exchangeRate but it should be return item.exchangeRate?

Updated fiddle: http://jsfiddle.net/WuvZD/4/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Asynchronous call inside a Knockout computed function

From Dev

Using recordset in function causes error 3420 in main sub code

From Dev

Data context for computed observable function in Knockout.js

From Dev

show loading div while Knockout computed function is running

From Dev

Running a function every time a computed observable changes Knockout

From Dev

knockout computed array not updating when function updates observable array used in computed

From Dev

Knockout Computed with Parameter not updated

From Dev

Knockout writable computed observables

From Dev

Knockout Computed Observable with parameters

From Dev

Knockout computed property not binding

From Dev

knockout foreach computed value

From Dev

knockout observable array computed

From Dev

knockout foreach computed value

From Dev

Knockout writable computed observables

From Dev

Knockout computed property not binding

From Dev

Reading byte array the second time causes error?

From Dev

Reading byte array the second time causes error?

From Dev

Knockout observable property contains function code

From Dev

Using Knockout push in a function returns an error

From Dev

observable and computed are not working properly Knockout

From Dev

Knockout custom binding to a computed column

From Dev

Knockout Js computed not working in a model

From Dev

Knockout js Computed not being fired

From Dev

Knockout computed observable with access to observableArray

From Dev

Knockout Modifying ObservableArray inside Computed

From Dev

Inheritance and overriding Knockout computed observable

From Dev

Knockout ViewModel computed garbage collection

From Dev

Subscribe arrayChange on knockout computed accessor

From Dev

Knockout ViewModel computed garbage collection

Related Related

  1. 1

    Asynchronous call inside a Knockout computed function

  2. 2

    Using recordset in function causes error 3420 in main sub code

  3. 3

    Data context for computed observable function in Knockout.js

  4. 4

    show loading div while Knockout computed function is running

  5. 5

    Running a function every time a computed observable changes Knockout

  6. 6

    knockout computed array not updating when function updates observable array used in computed

  7. 7

    Knockout Computed with Parameter not updated

  8. 8

    Knockout writable computed observables

  9. 9

    Knockout Computed Observable with parameters

  10. 10

    Knockout computed property not binding

  11. 11

    knockout foreach computed value

  12. 12

    knockout observable array computed

  13. 13

    knockout foreach computed value

  14. 14

    Knockout writable computed observables

  15. 15

    Knockout computed property not binding

  16. 16

    Reading byte array the second time causes error?

  17. 17

    Reading byte array the second time causes error?

  18. 18

    Knockout observable property contains function code

  19. 19

    Using Knockout push in a function returns an error

  20. 20

    observable and computed are not working properly Knockout

  21. 21

    Knockout custom binding to a computed column

  22. 22

    Knockout Js computed not working in a model

  23. 23

    Knockout js Computed not being fired

  24. 24

    Knockout computed observable with access to observableArray

  25. 25

    Knockout Modifying ObservableArray inside Computed

  26. 26

    Inheritance and overriding Knockout computed observable

  27. 27

    Knockout ViewModel computed garbage collection

  28. 28

    Subscribe arrayChange on knockout computed accessor

  29. 29

    Knockout ViewModel computed garbage collection

HotTag

Archive