CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Other Knockout.js: n00b can't calculate (http://www.codingforums.com/showthread.php?t=287983)

WolfShade 02-20-2013 02:33 PM

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,

WolfShade 02-20-2013 08:07 PM

Has _anyone_ successfully implemented calculations using Knockout.js?

WolfShade 02-22-2013 07:41 PM

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.


All times are GMT +1. The time now is 01:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.