Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts

    Question Knockout.js: n00b can't calculate

    Hello, everyone.

    I'm just now starting to learn Knockout, and I'm apparently unable to learn how to calculate values in Knockout. I've tried this several different ways, but my current code (pseudo-code) is:

    Code:
    In another <span data-bind="text: myAge"></span> years, I'll be <span data-bind="text: newAge"></span>
    <input type="text" data-bind="value: myAge">
    Code:
    function AppViewModel(){
      this.myAge = ko.observable(personalInfo.age);
      this.newAge = ko.computed(function(){return this.myAge*2;});
    }
    ko.applyBindings(new AppViewModel());
    text: newAge is resulting as NaN. If I alert this.myAge inside the AppViewModel(), it comes back as undefined. Didn't I just define it on the previous line?? In the SPAN tag of the sentence, it's appearing as 44

    What am I doing incorrectly?

    Thank you,
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Has _anyone_ successfully implemented calculations using Knockout.js?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Code:
    function AppViewModel(){
       this.myAge = ko.observable(personalInfo.age);
       this.newAge = ko.computed(function(){return this.myAge*2;},this);
     } 
    ko.applyBindings(new AppViewModel());
    The above fixed it.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •