Permalink
Browse files

Initial code for the optional todo #10 of part 3

  • Loading branch information...
1 parent e8c2ec6 commit 3026b4623d6508ba525fcfa94ca78da9fb12d474 @techarch committed Oct 29, 2011
Showing with 33 additions and 0 deletions.
  1. +1 −0 index.html
  2. +32 −0 scripts/viewmodel/sgs.model.savings-forecast.js
View
@@ -33,6 +33,7 @@
.script("scripts/viewmodel/sgs.model.coffee-consumption.js").wait()
.script("scripts/viewmodel/sgs.model.consumption-scenarios.js").wait()
.script("scripts/viewmediator/sgs.mediator.consumption-scenarios.js").wait()
+ .script("scripts/viewmodel/sgs.model.savings-forecast.js").wait()
.script("scripts/application.js")
.wait(function(){
// When ALL scripts have been loaded AND executed:
@@ -0,0 +1,32 @@
+// Lazy initialize our namespace context: sgs.model.savingsforecast
+if (typeof(sgs) == 'undefined') sgs = { }
+if (typeof(sgs.model) == 'undefined') sgs.model = { }
+if (typeof(sgs.model.savingsforecast) == 'undefined') sgs.model.savingsforecast = { }
+
+if (typeof(console) != 'undefined' && console) console.info("sgs.model.savingsforecast loading!");
+
+sgs.model.savingsforecast.initializeViewModel = function (pageSettings) {
+ // We can use properties of the pageSettings as default values for any of our ValueModels
+ // If pageSettings are not provided we'll initialize an empty object
+ if (typeof(pageSettings) == 'undefined') var pageSettings = { }
+
+ var viewModel = {
+ savingsGoalAmount: ko.observable(0),
+ savingsTargetPerMonth: ko.observable(0),
+ savingsPerMonth: ko.observable(0)
+ };
+
+ viewModel.forecastVariancePerMonth = ko.dependentObservable(function() {
+ var variance = this.savingsPerMonth() - this.savingsTargetPerMonth();
+ var result = Math.round(variance * 100) / 100;
+ return result;
+ }, viewModel);
+
+ viewModel.timeToGoalInMonths = ko.dependentObservable(function() {
+ var timeToGoal = this.savingsGoalAmount() / this.savingsPerMonth();
+ var result = Math.round(timeToGoal * 10) / 10;
+ return result;
+ }, viewModel);
+
+ return viewModel;
+}

0 comments on commit 3026b46

Please sign in to comment.